attempt to graft the main loop onto the sdl3 main module's structure

This commit is contained in:
2024-03-11 03:05:44 +11:00
parent 96d1ae11f7
commit c9e42f1206

View File

@ -11,26 +11,32 @@
#define WINDOW_HEIGHT 288 #define WINDOW_HEIGHT 288
int winw = WINDOW_WIDTH, winh = WINDOW_HEIGHT;
size rendSize;
vector plrpos = {10.0, 10.0};
StickState stickl, stickr;
static SDL_Window* window = NULL; static SDL_Window* window = NULL;
static SDL_JoystickID joyid = -1; static SDL_JoystickID joyid = -1;
static SDL_Gamepad* pad = NULL; static SDL_Gamepad* pad = NULL;
static Uint64 tickslast;
static bool UseGamepad(SDL_JoystickID aJoyid) static bool UseGamepad(SDL_JoystickID aJoyid)
{ {
pad = SDL_OpenGamepad(aJoyid); pad = SDL_OpenGamepad(aJoyid);
if (!pad) if (!pad)
return false; return false;
SDL_Log("using gamepad #%d, \"%s\"", aJoyid, SDL_GetGamepadName(pad));
joyid = aJoyid; joyid = aJoyid;
printf("using gamepad #%d, \"%s\"\n", aJoyid, SDL_GetGamepadName(pad));
return true; return true;
} }
#define FATAL(CONDITION, RETURN) if (CONDITION) { res = (RETURN); goto error; } #define FATAL(CONDITION) if (CONDITION) { return -1; }
int main(int argc, char** argv) int SDL_AppInit(int argc, char* argv[])
{ {
int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD); FATAL(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD) < 0)
if (res < 0)
goto error;
int winflg = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY; int winflg = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY;
#ifdef USE_OPENGL #ifdef USE_OPENGL
@ -38,17 +44,16 @@ int main(int argc, char** argv)
#elif defined USE_METAL #elif defined USE_METAL
winflg |= SDL_WINDOW_METAL; winflg |= SDL_WINDOW_METAL;
#endif #endif
int winw = WINDOW_WIDTH, winh = WINDOW_HEIGHT;
DrawWindowHints(); DrawWindowHints();
window = SDL_CreateWindow(CAPTION, winw, winh, winflg); window = SDL_CreateWindow(CAPTION, winw, winh, winflg);
FATAL(window == NULL, -1) FATAL(window == NULL)
FATAL(InitDraw(window), -1) FATAL(InitDraw(window))
size rendSize;
SDL_GetWindowSizeInPixels(window, &rendSize.w, &rendSize.h); SDL_GetWindowSizeInPixels(window, &rendSize.w, &rendSize.h);
if ((res = SDL_AddGamepadMappingsFromFile("gamecontrollerdb.txt")) != -1) int res = SDL_AddGamepadMappingsFromFile("gamecontrollerdb.txt");
printf("read %d mappings from gamecontrollerdb.txt\n", res); if (res != -1)
SDL_Log("read %d mappings from gamecontrollerdb.txt", res);
int numJoy; int numJoy;
SDL_JoystickID* joyIds = SDL_GetJoysticks(&numJoy); SDL_JoystickID* joyIds = SDL_GetJoysticks(&numJoy);
@ -60,129 +65,107 @@ int main(int argc, char** argv)
SDL_free(joyIds); SDL_free(joyIds);
} }
vector plrpos = {10.0, 10.0};
StickState stickl, stickr;
InitDefaults(&stickl); InitDefaults(&stickl);
InitDefaults(&stickr); InitDefaults(&stickr);
bool running = true; tickslast = SDL_GetTicks();
bool repaint = true;
bool showavatar = false;
uint64_t tickslast = SDL_GetTicks();
int side = 0;
while (running) return 0;
{ }
//FIXME: probably doesn't matter but this isn't very precise
const uint64_t ticks = SDL_GetTicks();
const double framedelta = (double)(ticks - tickslast);
tickslast = ticks;
SDL_Event event; static bool repaint = true;
bool onevent = false; static bool showavatar = false;
if (!showavatar || (stickl.compos.x == 0.0 && stickl.compos.y == 0.0)) static int side = 0;
int SDL_AppEvent(const SDL_Event* event)
{ {
onevent = SDL_WaitEvent(&event) != 0; switch (event->type)
}
else
{
onevent = SDL_PollEvent(&event) > 0;
repaint = true;
}
if (onevent)
{
do
{
switch (event.type)
{ {
case (SDL_EVENT_KEY_DOWN): case (SDL_EVENT_KEY_DOWN):
if (event.key.keysym.sym == SDLK_ESCAPE) if (event->key.keysym.sym == SDLK_ESCAPE)
{ return 1;
running = false; if (event->key.keysym.sym == SDLK_e)
}
else if (event.key.keysym.sym == SDLK_e)
{ {
showavatar = !showavatar; showavatar = !showavatar;
repaint = true; repaint = true;
} }
break; return 0;
case (SDL_EVENT_GAMEPAD_BUTTON_DOWN): case (SDL_EVENT_GAMEPAD_BUTTON_DOWN):
if (event.gbutton.button == SDL_GAMEPAD_BUTTON_BACK) if (event->gbutton.button == SDL_GAMEPAD_BUTTON_BACK)
{ {
showavatar = !showavatar; showavatar = !showavatar;
repaint = true; repaint = true;
} }
break; return 0;
case (SDL_EVENT_QUIT): case (SDL_EVENT_QUIT):
running = false; return 1;
break;
case (SDL_EVENT_WINDOW_RESIZED): case (SDL_EVENT_WINDOW_RESIZED):
winw = event.window.data1; winw = event->window.data1;
winh = event.window.data2; winh = event->window.data2;
break; return 0;
case (SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED): case (SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED):
rendSize = (size){event.window.data1, event.window.data2}; rendSize = (size){event->window.data1, event->window.data2};
SetDrawViewport(rendSize); SetDrawViewport(rendSize);
repaint = true; repaint = true;
break; return 0;
case (SDL_EVENT_WINDOW_EXPOSED): case (SDL_EVENT_WINDOW_EXPOSED):
repaint = true; repaint = true;
break; return 0;
case (SDL_EVENT_GAMEPAD_AXIS_MOTION): case (SDL_EVENT_GAMEPAD_AXIS_MOTION):
if (event.gaxis.which != joyid) if (event->gaxis.which != joyid)
break; return 0;
switch (event.gaxis.axis) switch (event->gaxis.axis)
{ {
case (SDL_GAMEPAD_AXIS_LEFTX): case (SDL_GAMEPAD_AXIS_LEFTX):
stickl.rawpos.x = (vec_t)event.gaxis.value / (vec_t)0x7FFF; stickl.rawpos.x = (vec_t)event->gaxis.value / (vec_t)0x7FFF;
repaint = stickl.recalc = true; repaint = stickl.recalc = true;
break; break;
case (SDL_GAMEPAD_AXIS_LEFTY): case (SDL_GAMEPAD_AXIS_LEFTY):
stickl.rawpos.y = (vec_t)event.gaxis.value / (vec_t)0x7FFF; stickl.rawpos.y = (vec_t)event->gaxis.value / (vec_t)0x7FFF;
repaint = stickl.recalc = true; repaint = stickl.recalc = true;
break; break;
case (SDL_GAMEPAD_AXIS_RIGHTX): case (SDL_GAMEPAD_AXIS_RIGHTX):
stickr.rawpos.x = (vec_t)event.gaxis.value / (vec_t)0x7FFF; stickr.rawpos.x = (vec_t)event->gaxis.value / (vec_t)0x7FFF;
repaint = stickr.recalc = true; repaint = stickr.recalc = true;
break; break;
case (SDL_GAMEPAD_AXIS_RIGHTY): case (SDL_GAMEPAD_AXIS_RIGHTY):
stickr.rawpos.y = (vec_t)event.gaxis.value / (vec_t)0x7FFF; stickr.rawpos.y = (vec_t)event->gaxis.value / (vec_t)0x7FFF;
repaint = stickr.recalc = true; repaint = stickr.recalc = true;
break; break;
default: default:
break; break;
} }
break; return 0;
case (SDL_EVENT_MOUSE_BUTTON_DOWN): case (SDL_EVENT_MOUSE_BUTTON_DOWN):
if (event.button.state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK)) if (event->button.state & (SDL_BUTTON_LMASK | SDL_BUTTON_RMASK))
side = (event.button.x > winw / 2) ? 1 : 0; side = (event->button.x > winw / 2) ? 1 : 0;
break; return 0;
case (SDL_EVENT_MOUSE_MOTION): case (SDL_EVENT_MOUSE_MOTION):
if (event.motion.state & SDL_BUTTON_LMASK) if (event->motion.state & SDL_BUTTON_LMASK)
{ {
const double hwinw = winw / 2.0; const double hwinw = winw / 2.0;
const double dispscale = 1.0 / (((hwinw > winh) ? winh : hwinw) * DISPLAY_SCALE / 2.0); const double dispscale = 1.0 / (((hwinw > winh) ? winh : hwinw) * DISPLAY_SCALE / 2.0);
const vector newpos = { const vector newpos = {
CLAMP(((double)event.motion.x - hwinw / 2.0 - hwinw * side) * dispscale, -1.0, 1.0), CLAMP(((double)event->motion.x - hwinw / 2.0 - hwinw * side) * dispscale, -1.0, 1.0),
CLAMP(((double)event.motion.y - winh / 2.0) * dispscale, -1.0, 1.0) }; CLAMP(((double)event->motion.y - winh / 2.0) * dispscale, -1.0, 1.0) };
StickState* stick = side ? &stickr : &stickl; StickState* stick = side ? &stickr : &stickl;
stick->rawpos = newpos; stick->rawpos = newpos;
repaint = stick->recalc = true; repaint = stick->recalc = true;
} }
else if (event.motion.state & SDL_BUTTON_RMASK) else if (event->motion.state & SDL_BUTTON_RMASK)
{ {
const double hwinw = winw / 2.0; const double hwinw = winw / 2.0;
const double valx = SATURATE(1.0 - (double)event.motion.x / (double)hwinw); const double valx = SATURATE(1.0 - (double)event->motion.x / (double)hwinw);
const double valy = SATURATE(1.0 - (double)event.motion.y / (double)winh); const double valy = SATURATE(1.0 - (double)event->motion.y / (double)winh);
if (side == 0) if (side == 0)
{ {
@ -197,31 +180,37 @@ int main(int argc, char** argv)
repaint = stickr.recalc = true; repaint = stickr.recalc = true;
} }
} }
break; return 0;
case (SDL_EVENT_GAMEPAD_ADDED): case (SDL_EVENT_GAMEPAD_ADDED):
if (pad == NULL) if (pad == NULL)
UseGamepad(event.gdevice.which); UseGamepad(event->gdevice.which);
break; return 0;
case (SDL_EVENT_GAMEPAD_REMOVED): case (SDL_EVENT_GAMEPAD_REMOVED):
if (pad != NULL && event.gdevice.which == joyid) if (pad != NULL && event->gdevice.which == joyid)
{ {
SDL_CloseGamepad(pad); SDL_CloseGamepad(pad);
pad = NULL; pad = NULL;
printf("active gamepad was removed\n"); SDL_Log("active gamepad was removed");
} }
break; return 0;
default: default:
break; return 0;
} }
} }
while (SDL_PollEvent(&event) > 0);
}
if (repaint) int SDL_AppIterate(void)
{ {
//FIXME: probably doesn't matter but this isn't very precise
const Uint64 ticks = SDL_GetTicks();
const double framedelta = (double)(ticks - tickslast);
tickslast = ticks;
if (!repaint)
return 0;
// background // background
SetDrawColour(GREY1); SetDrawColour(GREY1);
DrawClear(); DrawClear();
@ -249,14 +238,41 @@ int main(int argc, char** argv)
DrawPresent(); DrawPresent();
repaint = false; repaint = false;
} return 0;
} }
res = 0; void SDL_AppQuit(void)
error: {
SDL_CloseGamepad(pad); SDL_CloseGamepad(pad);
QuitDraw(); QuitDraw();
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
}
int main(int argc, char** argv)
{
int res = SDL_AppInit(argc, argv);
while (!res)
{
SDL_Event event;
bool onevent = false;
if (!showavatar || (stickl.compos.x == 0.0 && stickl.compos.y == 0.0))
{
onevent = SDL_WaitEvent(&event) != 0;
}
else
{
onevent = SDL_PollEvent(&event) > 0;
repaint = true;
}
if (onevent)
{
do res = SDL_AppEvent(&event);
while (!res && SDL_PollEvent(&event) > 0);
}
if (!res)
res = SDL_AppIterate();
}
SDL_AppQuit();
return res; return res;
} }