xormod/xormod.c

138 lines
3.8 KiB
C
Raw Normal View History

2024-12-30 18:28:05 +00:00
#include "font.h"
2021-04-12 19:15:41 +00:00
#include "field.h"
2024-12-30 11:39:12 +00:00
#include "text.h"
2021-04-12 19:15:41 +00:00
#include "picture.h"
2021-04-11 09:17:53 +00:00
#include "platform.h"
2021-04-12 19:15:41 +00:00
#include "types.h"
2021-04-10 19:30:51 +00:00
#include <stdio.h>
2025-01-07 15:59:33 +00:00
#include <inttypes.h>
2024-10-05 21:19:26 +00:00
#include <SDL3/SDL.h>
2021-04-10 19:30:51 +00:00
int main() {
2021-04-10 20:21:09 +00:00
int w = 1280, h = 720;
2021-04-10 19:30:51 +00:00
2024-10-05 21:19:26 +00:00
if (!SDL_Init(SDL_INIT_VIDEO)) {
2021-04-10 19:30:51 +00:00
fprintf(stderr, "Can't init: %s\n", SDL_GetError());
return -1;
}
2025-01-07 16:47:17 +00:00
SDL_Window *window = SDL_CreateWindow("xormod", w, h, ACCEL_PLATFORM | SDL_WINDOW_RESIZABLE);
2021-04-10 19:30:51 +00:00
if (window == NULL) {
fprintf(stderr, "Can't open window: %s\n", SDL_GetError());
return -1;
}
SDL_GetWindowSize(window, &w, &h);
2024-10-05 21:19:26 +00:00
SDL_Renderer *rend = SDL_CreateRenderer(window, NULL);
2021-04-10 19:30:51 +00:00
2021-04-11 15:21:31 +00:00
Field f;
2025-01-07 16:47:17 +00:00
Field_init(&f, w, h);
2021-04-10 19:30:51 +00:00
2024-10-05 21:19:26 +00:00
struct float2 mouse_prev = { -1, -1 };
2024-12-30 11:26:44 +00:00
Uint64 last = SDL_GetTicks();
2021-04-10 19:30:51 +00:00
int running = 1;
2025-01-07 16:49:29 +00:00
#ifdef NDEBUG
int debug = 0;
#else
2025-01-07 15:59:33 +00:00
int debug = 1;
2025-01-07 16:49:29 +00:00
#endif
2025-01-07 15:59:33 +00:00
2021-04-10 19:30:51 +00:00
while (running) {
2024-12-30 11:26:44 +00:00
Uint64 ticks = SDL_GetTicks();
Uint64 delta = ticks - last;
last = ticks;
2024-10-05 21:19:26 +00:00
const bool *keys = SDL_GetKeyboardState(NULL);
2024-12-30 10:20:09 +00:00
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) {
2021-04-11 15:21:31 +00:00
f.offset.x -= 16;
2021-04-10 19:30:51 +00:00
}
2024-12-30 10:20:09 +00:00
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) {
2021-04-11 15:21:31 +00:00
f.offset.x += 16;
2021-04-10 19:30:51 +00:00
}
2024-12-30 10:20:09 +00:00
if (keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]) {
2021-04-11 15:21:31 +00:00
f.offset.y -= 16;
2021-04-10 19:30:51 +00:00
}
2024-12-30 10:20:09 +00:00
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) {
2021-04-11 15:21:31 +00:00
f.offset.y += 16;
2021-04-10 19:30:51 +00:00
}
2024-10-05 21:19:26 +00:00
struct float2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON_LMASK) {
2021-04-10 19:30:51 +00:00
if (mouse_prev.x != -1 ) {
2021-04-11 15:21:31 +00:00
f.offset.x -= mouse.x - mouse_prev.x;
f.offset.y -= mouse.y - mouse_prev.y;
2021-04-10 19:30:51 +00:00
}
mouse_prev = mouse;
} else if (mouse_prev.x != -1) {
2024-10-05 21:19:26 +00:00
mouse_prev = (struct float2) { -1, -1 };
2021-04-10 19:30:51 +00:00
}
2025-01-07 16:47:17 +00:00
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderFillRect(rend, &(SDL_FRect) { 0, 0, w, h});
Field_scroll(&f);
2021-04-11 15:21:31 +00:00
Field_draw(&f, rend);
2021-04-10 19:30:51 +00:00
2025-01-07 15:59:33 +00:00
if (debug) {
2025-01-07 16:47:17 +00:00
char buf[72];
2025-01-07 15:59:33 +00:00
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderFillRect(rend, &(SDL_FRect) {
0, 0, sizeof(buf)*font_w, 8*font_h
});
draw_text(rend, ACCEL_PLATFORM_STR, sizeof(ACCEL_PLATFORM_STR), RC(1,1), 255, 255, 255);
size_t len = snprintf(buf, sizeof(buf),
2025-01-07 16:47:17 +00:00
"off.x: %d, off.y: %d, \xEBT: %02" PRIu64, f.offset.x, f.offset.y, delta);
2025-01-07 15:59:33 +00:00
draw_text(rend, buf, len, RC(2, 1), 255, 255, 255);
char *q[] = {"TL", "TR", "BL", "BR"};
for (int i = 0; i < 4; i++) {
Picture *p = f.pics[i];
2025-01-07 16:47:17 +00:00
len = snprintf(buf, sizeof(buf), "%s \xB3 x: %05d, y: %05d, w: %d, h: %d, p: %p",
q[i], p->x, p->y, p->w, p->h, p);
2025-01-07 15:59:33 +00:00
draw_text(rend, buf, len, RC(3+i, 1), 255, 255, 255);
}
2021-04-11 18:13:59 +00:00
}
2021-04-10 19:30:51 +00:00
SDL_RenderPresent(rend);
2025-01-07 15:59:33 +00:00
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: running = 0; break;
case SDL_EVENT_WINDOW_RESIZED: {
2025-01-07 16:47:17 +00:00
w = event.window.data1;
h = event.window.data2;
Field_resize(&f, w, h);
break;
}
2025-01-07 15:59:33 +00:00
case SDL_EVENT_KEY_DOWN: {
if (event.key.repeat) break;
switch (event.key.key) {
case SDLK_ESCAPE:
case SDLK_Q: running = 0; break;
case SDLK_G: debug ^= 1; break;
}
2025-01-07 16:47:17 +00:00
}
2025-01-07 15:59:33 +00:00
}
}
2024-12-30 11:26:44 +00:00
if (delta < 16) {
SDL_Delay(16 - delta);
}
2021-04-10 19:30:51 +00:00
}
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}