xormod/xormod.c

100 lines
2.7 KiB
C
Raw Normal View History

2021-04-10 19:30:51 +00:00
#include "EGA8x8.h"
2021-04-12 19:15:41 +00:00
#include "field.h"
#include "font.h"
#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>
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;
}
2024-10-05 21:19:26 +00:00
SDL_Window *window = SDL_CreateWindow("xormod", w, h, ACCEL_PLATFORM);
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
SDL_Texture *font = load_font(rend);
2021-04-11 15:21:31 +00:00
Field f;
Field_init(&f, rend, w, h);
2021-04-10 19:30:51 +00:00
2024-10-05 21:19:26 +00:00
struct float2 mouse_prev = { -1, -1 };
2021-04-10 19:30:51 +00:00
int running = 1;
while (running) {
2024-10-05 21:19:26 +00:00
const bool *keys = SDL_GetKeyboardState(NULL);
2021-04-10 19:30:51 +00:00
if (keys[SDL_SCANCODE_ESCAPE] || keys[SDL_SCANCODE_Q]) {
break;
}
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
}
2021-04-11 15:21:31 +00:00
Field_scroll(&f, rend);
Field_draw(&f, rend);
2021-04-10 19:30:51 +00:00
2021-04-11 18:13:59 +00:00
char buf[72];
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
2024-10-05 21:19:26 +00:00
SDL_RenderFillRect(rend, &(SDL_FRect) {
2021-04-11 18:13:59 +00:00
0, 0, sizeof(buf)*font_w, 8*font_h
});
2021-04-10 19:30:51 +00:00
2021-04-11 18:13:59 +00:00
draw_text(rend, font, ACCEL_PLATFORM_STR, sizeof(ACCEL_PLATFORM_STR), RC(1,1), 255, 255, 255);
2021-04-10 19:30:51 +00:00
2021-04-11 18:13:59 +00:00
size_t len = snprintf(buf, sizeof(buf),
"off.x: %d, off.y: %d", f.offset.x, f.offset.y);
draw_text(rend, font, 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| x1: %05d, x2: %05d, y1: %05d, y2: %05d, p: %p",
q[i], p->x1, p->x2, p->y1, p->y2, p);
draw_text(rend, font, buf, len, RC(3+i, 1), 255, 255, 255);
}
2021-04-10 19:30:51 +00:00
SDL_RenderPresent(rend);
2024-12-30 11:03:20 +00:00
SDL_PumpEvents();
2021-04-10 19:30:51 +00:00
}
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}