xormod/xormod.c

108 lines
2.9 KiB
C
Raw Permalink 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>
2021-04-10 20:21:09 +00:00
#include <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
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Can't init: %s\n", SDL_GetError());
return -1;
}
SDL_Window *window = SDL_CreateWindow("xormod",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h,
2021-12-01 23:01:48 +00:00
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);
SDL_Renderer *rend = SDL_CreateRenderer(window, -1,
2021-04-11 09:17:53 +00:00
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
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
struct int2 mouse_prev = { -1, -1 };
int running = 1;
while (running) {
const uint8_t *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE] || keys[SDL_SCANCODE_Q]) {
break;
}
if (keys[SDL_SCANCODE_LEFT]) {
2021-04-11 15:21:31 +00:00
f.offset.x -= 16;
2021-04-10 19:30:51 +00:00
}
if (keys[SDL_SCANCODE_RIGHT]) {
2021-04-11 15:21:31 +00:00
f.offset.x += 16;
2021-04-10 19:30:51 +00:00
}
if (keys[SDL_SCANCODE_UP]) {
2021-04-11 15:21:31 +00:00
f.offset.y -= 16;
2021-04-10 19:30:51 +00:00
}
if (keys[SDL_SCANCODE_DOWN]) {
2021-04-11 15:21:31 +00:00
f.offset.y += 16;
2021-04-10 19:30:51 +00:00
}
struct int2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) {
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) {
mouse_prev = (struct int2) { -1, -1 };
}
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);
SDL_RenderFillRect(rend, &(SDL_Rect) {
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);
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
running = 0;
}
}
}
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}