mouse works

This commit is contained in:
BADIM-PC\Vadim 2018-01-24 14:55:13 +03:00
parent 7e6eaccda6
commit ce0aef409b
3 changed files with 185 additions and 102 deletions

View File

@ -152,6 +152,83 @@ static void initTouchGamepad()
// updateGamepadParts(); // updateGamepadParts();
} }
static void calcTextureRect(SDL_Rect* rect)
{
SDL_GetWindowSize(platform.window, &rect->w, &rect->h);
if (rect->w * TIC80_HEIGHT < rect->h * TIC80_WIDTH)
{
s32 discreteWidth = rect->w - rect->w % TIC80_WIDTH;
s32 discreteHeight = TIC80_HEIGHT * discreteWidth / TIC80_WIDTH;
rect->x = (rect->w - discreteWidth) / 2;
rect->y = rect->w > rect->h
? (rect->h - discreteHeight) / 2
: OFFSET_LEFT*discreteWidth/TIC80_WIDTH;
rect->w = discreteWidth;
rect->h = discreteHeight;
}
else
{
s32 discreteHeight = rect->h - rect->h % TIC80_HEIGHT;
s32 discreteWidth = TIC80_WIDTH * discreteHeight / TIC80_HEIGHT;
rect->x = (rect->w - discreteWidth) / 2;
rect->y = (rect->h - discreteHeight) / 2;
rect->w = discreteWidth;
rect->h = discreteHeight;
}
}
static void processMouse()
{
s32 mx = 0, my = 0;
s32 mb = SDL_GetMouseState(&mx, &my);
tic_mem* tic = platform.studio->tic;
{
tic->ram.input.mouse.x = tic->ram.input.mouse.y = 0;
SDL_Rect rect = {0, 0, 0, 0};
calcTextureRect(&rect);
if(rect.w) tic->ram.input.mouse.x = (mx - rect.x) * TIC80_WIDTH / rect.w;
if(rect.h) tic->ram.input.mouse.y = (my - rect.y) * TIC80_HEIGHT / rect.h;
}
{
tic->ram.input.mouse.left = mb & SDL_BUTTON_LMASK;
tic->ram.input.mouse.middle = mb & SDL_BUTTON_MMASK;
tic->ram.input.mouse.right = mb & SDL_BUTTON_RMASK;
}
// for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
// {
// MouseState* state = &studioImpl.mouse.state[i];
// if(!state->down && (studioImpl.mouse.button & SDL_BUTTON(i + 1)))
// {
// state->down = true;
// state->start.x = studioImpl.mouse.cursor.x;
// state->start.y = studioImpl.mouse.cursor.y;
// }
// else if(state->down && !(studioImpl.mouse.button & SDL_BUTTON(i + 1)))
// {
// state->end.x = studioImpl.mouse.cursor.x;
// state->end.y = studioImpl.mouse.cursor.y;
// state->click = true;
// state->down = false;
// }
// }
}
static SDL_Event* pollEvent() static SDL_Event* pollEvent()
{ {
static SDL_Event event; static SDL_Event event;
@ -242,14 +319,14 @@ static SDL_Event* pollEvent()
break; break;
} }
return &event; // return &event;
} }
// if(platform.mode != TIC_RUN_MODE) // if(platform.mode != TIC_RUN_MODE)
// processGesture(); // processGesture();
// if(!platform.gesture.active) // if(!platform.gesture.active)
// processMouse(); processMouse();
// if(platform.mode == TIC_RUN_MODE) // if(platform.mode == TIC_RUN_MODE)
// { // {
@ -265,38 +342,6 @@ static SDL_Event* pollEvent()
return NULL; return NULL;
} }
static void calcTextureRect(SDL_Rect* rect)
{
SDL_GetWindowSize(platform.window, &rect->w, &rect->h);
if (rect->w * TIC80_HEIGHT < rect->h * TIC80_WIDTH)
{
s32 discreteWidth = rect->w - rect->w % TIC80_WIDTH;
s32 discreteHeight = TIC80_HEIGHT * discreteWidth / TIC80_WIDTH;
rect->x = (rect->w - discreteWidth) / 2;
rect->y = rect->w > rect->h
? (rect->h - discreteHeight) / 2
: OFFSET_LEFT*discreteWidth/TIC80_WIDTH;
rect->w = discreteWidth;
rect->h = discreteHeight;
}
else
{
s32 discreteHeight = rect->h - rect->h % TIC80_HEIGHT;
s32 discreteWidth = TIC80_WIDTH * discreteHeight / TIC80_HEIGHT;
rect->x = (rect->w - discreteWidth) / 2;
rect->y = (rect->h - discreteHeight) / 2;
rect->w = discreteWidth;
rect->h = discreteHeight;
}
}
static void blitTexture() static void blitTexture()
{ {
// tic_mem* tic = platform.tic; // tic_mem* tic = platform.tic;
@ -531,6 +576,9 @@ s32 main(s32 argc, char **argv)
if(platform.audio.cvt.buf) if(platform.audio.cvt.buf)
SDL_free(platform.audio.cvt.buf); SDL_free(platform.audio.cvt.buf);
// if(platform.mouse.texture)
// SDL_DestroyTexture(platform.mouse.texture);
SDL_DestroyTexture(platform.texture); SDL_DestroyTexture(platform.texture);
SDL_DestroyRenderer(platform.renderer); SDL_DestroyRenderer(platform.renderer);
SDL_DestroyWindow(platform.window); SDL_DestroyWindow(platform.window);

View File

@ -112,13 +112,13 @@ static struct
struct struct
{ {
SDL_Point cursor; // SDL_Point cursor;
u32 button; // u32 button;
MouseState state[3]; MouseState state[3];
SDL_Texture* texture; // SDL_Texture* texture;
const u8* src; // const u8* src;
SDL_SystemCursor system; SDL_SystemCursor system;
} mouse; } mouse;
@ -254,14 +254,14 @@ static struct
.prevMode = TIC_CODE_MODE, .prevMode = TIC_CODE_MODE,
.dialogMode = TIC_CONSOLE_MODE, .dialogMode = TIC_CONSOLE_MODE,
.mouse = // .mouse =
{ // {
.cursor = {-1, -1}, // .cursor = {-1, -1},
.button = 0, // .button = 0,
.src = NULL, // .src = NULL,
.texture = NULL, // .texture = NULL,
.system = SDL_SYSTEM_CURSOR_ARROW, // .system = SDL_SYSTEM_CURSOR_ARROW,
}, // },
.gesture = .gesture =
{ {
@ -1058,17 +1058,22 @@ void hideGameMenu()
s32 getMouseX() s32 getMouseX()
{ {
return studioImpl.mouse.cursor.x; tic_mem* tic = studioImpl.studio.tic;
return tic->ram.input.mouse.x;
} }
s32 getMouseY() s32 getMouseY()
{ {
return studioImpl.mouse.cursor.y; tic_mem* tic = studioImpl.studio.tic;
return tic->ram.input.mouse.y;
} }
bool checkMousePos(const SDL_Rect* rect) bool checkMousePos(const SDL_Rect* rect)
{ {
return SDL_PointInRect(&studioImpl.mouse.cursor, rect); SDL_Point pos = {getMouseX(), getMouseY()};
return SDL_PointInRect(&pos, rect);
} }
bool checkMouseClick(const SDL_Rect* rect, s32 button) bool checkMouseClick(const SDL_Rect* rect, s32 button)
@ -1562,39 +1567,39 @@ static void processGesture()
} }
} }
static void processMouse() // static void processMouse()
{ // {
// studioImpl.mouse.button = SDL_GetMouseState(&studioImpl.mouse.cursor.x, &studioImpl.mouse.cursor.y); // studioImpl.mouse.button = SDL_GetMouseState(&studioImpl.mouse.cursor.x, &studioImpl.mouse.cursor.y);
// { // {
// SDL_Rect rect = {0, 0, 0, 0}; // SDL_Rect rect = {0, 0, 0, 0};
// calcTextureRect(&rect); // calcTextureRect(&rect);
// if(rect.w) studioImpl.mouse.cursor.x = (studioImpl.mouse.cursor.x - rect.x) * TIC80_WIDTH / rect.w; // if(rect.w) studioImpl.mouse.cursor.x = (studioImpl.mouse.cursor.x - rect.x) * TIC80_WIDTH / rect.w;
// if(rect.h) studioImpl.mouse.cursor.y = (studioImpl.mouse.cursor.y - rect.y) * TIC80_HEIGHT / rect.h; // if(rect.h) studioImpl.mouse.cursor.y = (studioImpl.mouse.cursor.y - rect.y) * TIC80_HEIGHT / rect.h;
// } // }
// for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++) // for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
// { // {
// MouseState* state = &studioImpl.mouse.state[i]; // MouseState* state = &studioImpl.mouse.state[i];
// if(!state->down && (studioImpl.mouse.button & SDL_BUTTON(i + 1))) // if(!state->down && (studioImpl.mouse.button & SDL_BUTTON(i + 1)))
// { // {
// state->down = true; // state->down = true;
// state->start.x = studioImpl.mouse.cursor.x; // state->start.x = studioImpl.mouse.cursor.x;
// state->start.y = studioImpl.mouse.cursor.y; // state->start.y = studioImpl.mouse.cursor.y;
// } // }
// else if(state->down && !(studioImpl.mouse.button & SDL_BUTTON(i + 1))) // else if(state->down && !(studioImpl.mouse.button & SDL_BUTTON(i + 1)))
// { // {
// state->end.x = studioImpl.mouse.cursor.x; // state->end.x = studioImpl.mouse.cursor.x;
// state->end.y = studioImpl.mouse.cursor.y; // state->end.y = studioImpl.mouse.cursor.y;
// state->click = true; // state->click = true;
// state->down = false; // state->down = false;
// } // }
// } // }
} // }
static void goFullscreen() static void goFullscreen()
{ {
@ -1890,25 +1895,25 @@ static void processGamepadInput()
processGamepad(); processGamepad();
} }
static void processMouseInput() // static void processMouseInput()
{ // {
processJoysticksWithMouseInput(); // processJoysticksWithMouseInput();
s32 x = studioImpl.mouse.cursor.x; // s32 x = studioImpl.mouse.cursor.x;
s32 y = studioImpl.mouse.cursor.y; // s32 y = studioImpl.mouse.cursor.y;
if(x < 0) x = 0; // if(x < 0) x = 0;
if(y < 0) y = 0; // if(y < 0) y = 0;
if(x >= TIC80_WIDTH) x = TIC80_WIDTH-1; // if(x >= TIC80_WIDTH) x = TIC80_WIDTH-1;
if(y >= TIC80_HEIGHT) y = TIC80_HEIGHT-1; // if(y >= TIC80_HEIGHT) y = TIC80_HEIGHT-1;
tic80_mouse* mouse = &studioImpl.studio.tic->ram.input.mouse; // tic80_mouse* mouse = &studioImpl.studio.tic->ram.input.mouse;
mouse->x = x; // mouse->x = x;
mouse->y = y; // mouse->y = y;
mouse->left = studioImpl.mouse.state[0].down ? 1 : 0; // mouse->left = studioImpl.mouse.state[0].down ? 1 : 0;
mouse->middle = studioImpl.mouse.state[1].down ? 1 : 0; // mouse->middle = studioImpl.mouse.state[1].down ? 1 : 0;
mouse->right = studioImpl.mouse.state[2].down ? 1 : 0; // mouse->right = studioImpl.mouse.state[2].down ? 1 : 0;
} // }
static void processKeyboardInput() static void processKeyboardInput()
{ {
@ -2358,9 +2363,9 @@ static void renderStudio()
showTooltip(""); showTooltip("");
studioImpl.gesture.active = false; studioImpl.gesture.active = false;
studioImpl.mouse.cursor.x = studioImpl.mouse.cursor.y = -1; // studioImpl.mouse.cursor.x = studioImpl.mouse.cursor.y = -1;
for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++) // for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
studioImpl.mouse.state[i].click = false; // studioImpl.mouse.state[i].click = false;
{ {
const tic_sfx* sfx = NULL; const tic_sfx* sfx = NULL;
@ -2836,8 +2841,39 @@ Studio* studioInit(s32 argc, char **argv, s32 samplerate)
return &studioImpl.studio; return &studioImpl.studio;
} }
static void processMouseStates()
{
for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
studioImpl.mouse.state[i].click = false;
tic_mem* tic = studioImpl.studio.tic;
for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
{
MouseState* state = &studioImpl.mouse.state[i];
if(!state->down && tic->ram.input.mouse.btns & (1 << i))
{
state->down = true;
state->start.x = tic->ram.input.mouse.x;
state->start.y = tic->ram.input.mouse.y;
}
else if(state->down && tic->ram.input.mouse.btns & (1 << i))
{
state->end.x = tic->ram.input.mouse.x;
state->end.y = tic->ram.input.mouse.y;
state->click = true;
state->down = false;
}
}
}
void studioTick(void* pixels) void studioTick(void* pixels)
{ {
processMouseStates();
renderStudio(); renderStudio();
tic_mem* tic = studioImpl.studio.tic; tic_mem* tic = studioImpl.studio.tic;
@ -2912,17 +2948,17 @@ void studioClose()
SDL_DestroyTexture(studioImpl.gamepad.texture); SDL_DestroyTexture(studioImpl.gamepad.texture);
// SDL_DestroyTexture(studioImpl.texture); // SDL_DestroyTexture(studioImpl.texture);
if(studioImpl.mouse.texture) // if(studioImpl.mouse.texture)
SDL_DestroyTexture(studioImpl.mouse.texture); // SDL_DestroyTexture(studioImpl.mouse.texture);
// SDL_DestroyRenderer(studioImpl.renderer); // SDL_DestroyRenderer(studioImpl.renderer);
// SDL_DestroyWindow(studioImpl.window); // SDL_DestroyWindow(studioImpl.window);
#if !defined (__MACOSX__) // #if !defined (__MACOSX__)
// stucks here on macos // stucks here on macos
// SDL_CloseAudioDevice(studioImpl.audio.device); // SDL_CloseAudioDevice(studioImpl.audio.device);
SDL_Quit(); // SDL_Quit();
#endif // #endif
exit(0); // exit(0);
} }

View File

@ -199,7 +199,6 @@ void runProject();
tic_tiles* getBankTiles(); tic_tiles* getBankTiles();
tic_map* getBankMap(); tic_map* getBankMap();
typedef struct typedef struct
{ {
tic_mem* tic; tic_mem* tic;