xormod/xormod.c

133 lines
3.8 KiB
C

#include "font.h"
#include "field.h"
#include "text.h"
#include "picture.h"
#include "platform.h"
#include "types.h"
#include <stdio.h>
#include <inttypes.h>
#include <SDL3/SDL.h>
int main() {
int w = 1280, h = 720;
if (!SDL_Init(SDL_INIT_VIDEO)) {
fprintf(stderr, "Can't init: %s\n", SDL_GetError());
return -1;
}
SDL_Window *window = SDL_CreateWindow("xormod", w, h, ACCEL_PLATFORM | SDL_WINDOW_RESIZABLE);
if (window == NULL) {
fprintf(stderr, "Can't open window: %s\n", SDL_GetError());
return -1;
}
SDL_GetWindowSize(window, &w, &h);
SDL_Renderer *rend = SDL_CreateRenderer(window, NULL);
Field f;
Field_init(&f, w, h);
struct float2 mouse_prev = { -1, -1 };
Uint64 last = SDL_GetTicks();
int running = 1;
int debug = 1;
while (running) {
Uint64 ticks = SDL_GetTicks();
Uint64 delta = ticks - last;
last = ticks;
const bool *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) {
f.offset.x -= 16;
}
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) {
f.offset.x += 16;
}
if (keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]) {
f.offset.y -= 16;
}
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) {
f.offset.y += 16;
}
struct float2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON_LMASK) {
if (mouse_prev.x != -1 ) {
f.offset.x -= mouse.x - mouse_prev.x;
f.offset.y -= mouse.y - mouse_prev.y;
}
mouse_prev = mouse;
} else if (mouse_prev.x != -1) {
mouse_prev = (struct float2) { -1, -1 };
}
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderFillRect(rend, &(SDL_FRect) { 0, 0, w, h});
Field_scroll(&f);
Field_draw(&f, rend);
if (debug) {
char buf[72];
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),
"off.x: %d, off.y: %d, \xEBT: %02" PRIu64, f.offset.x, f.offset.y, delta);
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];
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);
draw_text(rend, buf, len, RC(3+i, 1), 255, 255, 255);
}
}
SDL_RenderPresent(rend);
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: running = 0; break;
case SDL_EVENT_WINDOW_RESIZED: {
w = event.window.data1;
h = event.window.data2;
Field_resize(&f, w, h);
break;
}
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;
}
}
}
}
if (delta < 16) {
SDL_Delay(16 - delta);
}
}
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}