xormod/xormod.c

108 lines
2.9 KiB
C

#include "EGA8x8.h"
#include "field.h"
#include "font.h"
#include "picture.h"
#include "platform.h"
#include "types.h"
#include <stdio.h>
#include <SDL.h>
int main() {
int w = 1280, h = 720;
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,
ACCEL_PLATFORM | SDL_WINDOW_FULLSCREEN);
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,
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
SDL_Texture *font = load_font(rend);
Field f;
Field_init(&f, rend, w, h);
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]) {
f.offset.x -= 16;
}
if (keys[SDL_SCANCODE_RIGHT]) {
f.offset.x += 16;
}
if (keys[SDL_SCANCODE_UP]) {
f.offset.y -= 16;
}
if (keys[SDL_SCANCODE_DOWN]) {
f.offset.y += 16;
}
struct int2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) {
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 int2) { -1, -1 };
}
Field_scroll(&f, rend);
Field_draw(&f, rend);
char buf[72];
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderFillRect(rend, &(SDL_Rect) {
0, 0, sizeof(buf)*font_w, 8*font_h
});
draw_text(rend, font, 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", 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);
}
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;
}