crocparty/sdl_host/main.c

189 lines
4.9 KiB
C
Raw Normal View History

2024-02-25 03:00:23 +00:00
#include <stdio.h>
#include <stdbool.h>
2024-02-25 21:49:21 +00:00
// don't use sdl's redefinition of main
#define SDL_MAIN_HANDLED
2024-02-25 05:04:20 +00:00
#include "sdl/include/SDL.h"
2024-02-26 00:40:33 +00:00
#include "device/device.h"
2024-02-25 21:49:21 +00:00
#include "game/game.h"
2024-02-25 03:00:23 +00:00
void sdl_host_suggest_dimensions(uint32_t* window_w, uint32_t* window_h);
void sdl_host_loop();
2024-02-26 00:25:34 +00:00
void sdl_host_handle_keyboard_event(SDL_KeyboardEvent event);
2024-02-25 03:00:23 +00:00
SDL_Window* sdl_host_window;
SDL_Renderer* sdl_host_renderer;
SDL_Texture* sdl_host_target;
2024-02-25 21:49:21 +00:00
int main(int argc, char** argv) {
2024-02-25 03:00:23 +00:00
int result = 0;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)) {
printf("could not initialize SDL! sdl error: %s\n", SDL_GetError());
result = 1;
goto sdl_done;
}
uint32_t window_w, window_h;
sdl_host_suggest_dimensions(&window_w, &window_h);
// create window
sdl_host_window = SDL_CreateWindow(
game_title(),
2024-02-25 03:00:23 +00:00
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
(int) window_w, (int) window_h,
SDL_WINDOW_SHOWN
);
if (sdl_host_window == NULL) {
2024-02-25 03:04:23 +00:00
printf("could not create window! sdl error: %s\n", SDL_GetError());
2024-02-25 03:00:23 +00:00
result = 1;
goto window_done;
}
// create renderer
sdl_host_renderer = SDL_CreateRenderer(sdl_host_window, -1, 0);
if (sdl_host_renderer == NULL) {
2024-02-25 03:04:23 +00:00
printf("could not create renderer! sdl error: %s\n", SDL_GetError());
2024-02-25 03:00:23 +00:00
result = 1;
goto renderer_done;
}
// create target
sdl_host_target = SDL_CreateTexture(
sdl_host_renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
DEVICE_W, DEVICE_H
);
if (sdl_host_target == NULL) {
printf("could not create target texture! sdl error: %s\n", SDL_GetError());
result = 1;
goto target_done;
}
sdl_host_loop();
// renderer_cleanup:
SDL_DestroyRenderer(sdl_host_renderer);
sdl_host_renderer = NULL;
renderer_done: ;
// target_cleanup:
SDL_DestroyTexture(sdl_host_target);
sdl_host_target = NULL;
target_done: ;
// window_cleanup:
SDL_DestroyWindow(sdl_host_window);
sdl_host_window = NULL;
window_done: ;
// sdl_cleanup:
SDL_Quit();
sdl_done: ;
return result;
}
void sdl_host_suggest_dimensions(uint32_t* window_w, uint32_t* window_h) {
SDL_DisplayMode dm;
if (SDL_GetCurrentDisplayMode(0, &dm) != 0) {
printf("could not get current display mode: %s\n", SDL_GetError());
dm.w = 0;
dm.h = 0;
}
2024-02-26 23:27:05 +00:00
for (int scalar = 2; scalar >= 1; scalar--) {
2024-02-25 03:00:23 +00:00
uint32_t w = DEVICE_W * scalar;
uint32_t h = DEVICE_H * scalar;
if (w <= dm.w && h <= dm.h) {
*window_w = w;
*window_h = h;
return;
}
}
for (int scalar = 1; scalar <= 5; scalar++) {
uint32_t w = DEVICE_W / scalar;
uint32_t h = DEVICE_H / scalar;
if (w <= dm.w && h <= dm.h) {
*window_w = w;
*window_h = h;
return;
}
}
*window_w = DEVICE_W;
*window_h = DEVICE_H;
}
void sdl_host_loop() {
uint32_t ticks_per_frame = 1000/60;
2024-02-26 01:54:57 +00:00
uint32_t real_pixels[DEVICE_H][DEVICE_W];
2024-02-25 03:00:23 +00:00
game_init();
while (true) {
uint32_t frame_start = SDL_GetTicks();
uint32_t next_frame_start = frame_start + ticks_per_frame;
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) { goto quit; }
2024-02-26 00:25:34 +00:00
if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
sdl_host_handle_keyboard_event(e.key);
}
2024-02-25 03:00:23 +00:00
}
// trigger game logic
game_update();
game_draw();
2024-02-26 01:54:57 +00:00
for (int x = 0; x < DEVICE_W; x++) {
for (int y = 0; y < DEVICE_H; y++) {
real_pixels[y][x] = device_palette[device_pixels[y][x]];
}
}
SDL_UpdateTexture(sdl_host_target, NULL, &real_pixels, sizeof(real_pixels[0]));
2024-02-25 03:00:23 +00:00
SDL_RenderClear(sdl_host_renderer);
SDL_RenderCopy(sdl_host_renderer, sdl_host_target, NULL, NULL);
SDL_RenderPresent(sdl_host_renderer);
// hold off until next frame
while (SDL_GetTicks() < next_frame_start) {
SDL_Delay(1);
}
}
quit: ;
2024-02-26 00:25:34 +00:00
}
void sdl_host_handle_keyboard_event(SDL_KeyboardEvent event) {
bool is_down;
switch (event.type) {
case SDL_KEYDOWN: is_down = true; break;
case SDL_KEYUP: is_down = false; break;
default: return;
}
if (event.repeat) {
return;
}
DeviceButton db;
switch (event.keysym.sym) {
case SDLK_LEFT: db = DEVICE_BUTTON_L; break;
case SDLK_RIGHT: db = DEVICE_BUTTON_R; break;
case SDLK_UP: db = DEVICE_BUTTON_U; break;
case SDLK_DOWN: db = DEVICE_BUTTON_D; break;
case SDLK_z: db = DEVICE_BUTTON_0; break;
case SDLK_x: db = DEVICE_BUTTON_1; break;
case SDLK_c: db = DEVICE_BUTTON_2; break;
case SDLK_v: db = DEVICE_BUTTON_3; break;
default: return;
}
device_buttons[db] = is_down;
2024-02-25 03:00:23 +00:00
}