xormod/xormod.c

245 lines
6.5 KiB
C
Raw Normal View History

2021-04-10 19:30:51 +00:00
#include "font.h"
#include "EGA8x8.h"
2021-04-11 09:17:53 +00:00
#include "platform.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
#define SWAP(a, b) ({ \
__typeof(a) tmp = (a); \
(a) = (b); \
(b) = tmp; \
})
typedef struct {
int x1;
int y1;
int x2;
int y2;
int w;
int h;
2021-04-11 20:29:54 +00:00
int s;
2021-04-10 19:30:51 +00:00
SDL_Surface *surface;
SDL_Texture *texture;
uint32_t *pixels;
uint32_t color;
} Picture;
static void Picture_render(Picture *p, SDL_Renderer *rend);
static Picture *Picture_new(SDL_Renderer *rend,
int x, int y, int w, int h, uint32_t color) {
Picture *p = calloc(1, sizeof(Picture));
p->x1 = x;
p->x2 = x + w;
p->y1 = y;
p->y2 = y + h;
p->w = w;
p->h = h;
p->color = color;
2021-04-11 20:29:54 +00:00
p->s = (w + 7) / 8 * 8;
2021-04-10 19:30:51 +00:00
2021-04-11 20:29:54 +00:00
p->pixels = malloc(p->s * h * sizeof(uint32_t));
2021-04-10 19:30:51 +00:00
p->surface = SDL_CreateRGBSurfaceWithFormatFrom(
2021-04-11 20:29:54 +00:00
p->pixels, w, h, 32, p->s * sizeof(uint32_t),
2021-04-10 19:30:51 +00:00
SDL_PIXELFORMAT_RGBA32);
Picture_render(p, rend);
return p;
}
2021-04-10 20:21:09 +00:00
typedef uint32_t uint32_8 __attribute__ ((vector_size (8 * sizeof(uint32_t))));
2021-04-10 19:30:51 +00:00
static void Picture_render(Picture *p, SDL_Renderer *rend) {
2021-04-11 18:22:11 +00:00
int w = p->w, h = p->h;
2021-04-10 20:21:09 +00:00
uint32_8 c = p->color - (uint32_8){};
2021-04-11 18:22:11 +00:00
uint32_8 y = p->y1 - (uint32_8){};
for (int j = 0, r = 0; j < h; j++, y += 1) {
uint32_8 x = p->x1 + (uint32_8){0, 1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < w; i += 8,r += 8, x += 8) {
2021-04-10 20:21:09 +00:00
uint32_8 z = (x ^ y) % 9;
uint32_8 f = (z==0) & c;
2021-04-10 19:30:51 +00:00
memcpy(&p->pixels[r], (uint32_t*)&f, sizeof(f));
}
}
if (p->texture) SDL_DestroyTexture(p->texture);
p->texture = SDL_CreateTextureFromSurface(rend, p->surface);
SDL_SetTextureBlendMode(p->texture, SDL_BLENDMODE_NONE);
}
static void Picture_move(Picture *p, int x, int y) {
p->x1 += x;
p->x2 += x;
p->y1 += y;
p->y2 += y;
}
struct int2 { int x; int y; };
2021-04-11 15:21:31 +00:00
typedef struct {
union {
struct {
Picture *tl;
Picture *tr;
Picture *bl;
Picture *br;
};
Picture *pics[4];
};
struct int2 offset;
} Field;
void Field_init(Field *f, SDL_Renderer *rend, int w, int h) {
f->tl = Picture_new(rend, 0, 0, w, h, 0xff7fffff);
f->tr = Picture_new(rend, w, 0, w, h, 0xffff7fff);
f->bl = Picture_new(rend, 0, h, w, h, 0xffffff7f);
f->br = Picture_new(rend, w, h, w, h, 0xff7fff7f);
f->offset = (struct int2){ 0, 0 };
}
void Field_scroll(Field *f, SDL_Renderer *rend) {
int w = f->pics[0]->w;
int h = f->pics[0]->h;
if (f->offset.y < f->tl->y1) {
SWAP(f->tl, f->bl);
SWAP(f->tr, f->br);
Picture_move(f->tl, 0, -2*h);
Picture_move(f->tr, 0, -2*h);
Picture_render(f->tl, rend);
Picture_render(f->tr, rend);
}
if (f->offset.x < f->tl->x1) {
SWAP(f->tl, f->tr);
SWAP(f->bl, f->br);
Picture_move(f->tl, -2*w, 0);
Picture_move(f->bl, -2*w, 0);
Picture_render(f->tl, rend);
Picture_render(f->bl, rend);
}
if (f->offset.y > f->br->y1) {
SWAP(f->tl, f->bl);
SWAP(f->tr, f->br);
Picture_move(f->bl, 0, 2*h);
Picture_move(f->br, 0, 2*h);
Picture_render(f->bl, rend);
Picture_render(f->br, rend);
}
if (f->offset.x > f->br->x1) {
SWAP(f->tl, f->tr);
SWAP(f->bl, f->br);
Picture_move(f->tr, 2*w, 0);
Picture_move(f->br, 2*w, 0);
Picture_render(f->tr, rend);
Picture_render(f->br, rend);
}
}
void Field_draw(Field *f, SDL_Renderer *rend) {
for (int i = 0; i < 4; i++) {
Picture *p = f->pics[i];
SDL_Rect dst = {
p->x1 - f->offset.x, p->y1 - f->offset.y, p->w, p->h,
};
SDL_RenderCopy(rend, p->texture, NULL, &dst);
}
}
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-04-11 09:17:53 +00:00
ACCEL_PLATFORM | SDL_WINDOW_FULLSCREEN);
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;
}