245 lines
6.5 KiB
C
245 lines
6.5 KiB
C
#include "font.h"
|
|
#include "EGA8x8.h"
|
|
#include "platform.h"
|
|
|
|
#include <stdio.h>
|
|
#include <SDL.h>
|
|
|
|
#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;
|
|
int s;
|
|
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;
|
|
p->s = (w + 7) / 8 * 8;
|
|
|
|
p->pixels = malloc(p->s * h * sizeof(uint32_t));
|
|
p->surface = SDL_CreateRGBSurfaceWithFormatFrom(
|
|
p->pixels, w, h, 32, p->s * sizeof(uint32_t),
|
|
SDL_PIXELFORMAT_RGBA32);
|
|
|
|
Picture_render(p, rend);
|
|
return p;
|
|
}
|
|
|
|
typedef uint32_t uint32_8 __attribute__ ((vector_size (8 * sizeof(uint32_t))));
|
|
|
|
static void Picture_render(Picture *p, SDL_Renderer *rend) {
|
|
int w = p->w, h = p->h;
|
|
uint32_8 c = p->color - (uint32_8){};
|
|
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) {
|
|
uint32_8 z = (x ^ y) % 9;
|
|
uint32_8 f = (z==0) & c;
|
|
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; };
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|