port to SDL3

main
Henri Vasserman 2024-10-06 00:19:26 +03:00
parent 3e71b3d5c5
commit 85281120a3
8 changed files with 41 additions and 34 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.18)
project(xormod C)
find_package(SDL2)
find_package(SDL3)
if(APPLE)
set(DETECTED_ACCEL_PLATFORM "METAL")
@ -26,4 +26,4 @@ add_executable(xormod
"xormod.c"
)
target_link_libraries(xormod SDL2)
target_link_libraries(xormod SDL3)

View File

@ -1,6 +1,6 @@
#include "field.h"
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#define SWAP(a, b) ({ \
__typeof(a) tmp = (a); \
@ -56,10 +56,10 @@ void Field_scroll(Field *f, SDL_Renderer *rend) {
void Field_draw(Field *f, SDL_Renderer *rend) {
for (int i = 0; i < 4; i++) {
Picture *p = f->pics[i];
SDL_Rect dst = {
SDL_FRect dst = {
p->x1 - f->offset.x, p->y1 - f->offset.y, p->w, p->h,
};
SDL_RenderCopy(rend, p->texture, NULL, &dst);
SDL_RenderTexture(rend, p->texture, NULL, &dst);
}
}

12
font.c
View File

@ -1,12 +1,12 @@
#include "EGA8x8.h"
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
SDL_Texture *load_font(SDL_Renderer *renderer) {
SDL_Surface *s = SDL_LoadBMP_RW(SDL_RWFromConstMem(font_data, font_data_len), 1);
SDL_SetColorKey(s, SDL_TRUE, SDL_MapRGB(s->format, 255, 0, 255));
SDL_Surface *s = SDL_LoadBMP_IO(SDL_IOFromConstMem(font_data, font_data_len), true);
SDL_SetSurfaceColorKey(s, true, SDL_MapSurfaceRGB(s, 255, 0, 255));
SDL_Texture *font_texture = SDL_CreateTextureFromSurface(renderer, s);
SDL_FreeSurface(s);
SDL_DestroySurface(s);
return font_texture;
}
@ -14,7 +14,7 @@ void draw_text(SDL_Renderer *renderer, SDL_Texture *font,
char *text, size_t len, int x, int y, uint8_t r, uint8_t g, uint8_t b) {
if (font == NULL) return;
SDL_SetTextureColorMod(font, r, g, b);
SDL_Rect src = { .w = font_w, .h = font_h },
SDL_FRect src = { .w = font_w, .h = font_h },
dst = { .x = x, .y = y, .w = font_w, .h = font_h };
for (size_t i = 0; i < len; i++) {
char c = text[i];
@ -26,7 +26,7 @@ void draw_text(SDL_Renderer *renderer, SDL_Texture *font,
}
src.x = (c & 0xf) * font_w;
src.y = (c >> 4) * font_h;
SDL_RenderCopy(renderer, font, &src, &dst);
SDL_RenderTexture(renderer, font, &src, &dst);
dst.x += font_w;
}

2
font.h
View File

@ -1,6 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#define RC(r, c) (c)*font_w, (r)*font_h

View File

@ -1,3 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "picture.h"
Picture *Picture_new(SDL_Renderer *rend,
@ -12,13 +15,14 @@ Picture *Picture_new(SDL_Renderer *rend,
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);
p->s = (w * sizeof(uint32_t) + 7) / 8 * 8;
p->pixels = malloc(p->s * h);
p->surface = SDL_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGBA32, p->pixels, p->s);
if (p->surface == NULL) {
fprintf(stderr, "Can't create surface: %s\n", SDL_GetError());
exit(-1);
}
Picture_render(p, rend);
return p;
}
@ -41,6 +45,10 @@ void Picture_render(Picture *p, SDL_Renderer *rend) {
}
if (p->texture) SDL_DestroyTexture(p->texture);
p->texture = SDL_CreateTextureFromSurface(rend, p->surface);
if (p->texture == NULL) {
fprintf(stderr, "Can't create texture: %s\n", SDL_GetError());
exit(-1);
}
SDL_SetTextureBlendMode(p->texture, SDL_BLENDMODE_NONE);
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <stdint.h>
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
typedef struct {
int x1;

View File

@ -1,3 +1,4 @@
#pragma once
struct int2 { int x; int y; };
struct int2 { int x, y; };
struct float2 { float x, y; };

View File

@ -6,20 +6,18 @@
#include "types.h"
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
int main() {
int w = 1280, h = 720;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
if (!SDL_Init(SDL_INIT_VIDEO)) {
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 *window = SDL_CreateWindow("xormod", w, h, ACCEL_PLATFORM);
if (window == NULL) {
fprintf(stderr, "Can't open window: %s\n", SDL_GetError());
return -1;
@ -27,18 +25,17 @@ int main() {
SDL_GetWindowSize(window, &w, &h);
SDL_Renderer *rend = SDL_CreateRenderer(window, -1,
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
SDL_Renderer *rend = SDL_CreateRenderer(window, NULL);
SDL_Texture *font = load_font(rend);
Field f;
Field_init(&f, rend, w, h);
struct int2 mouse_prev = { -1, -1 };
struct float2 mouse_prev = { -1, -1 };
int running = 1;
while (running) {
const uint8_t *keys = SDL_GetKeyboardState(NULL);
const bool *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_ESCAPE] || keys[SDL_SCANCODE_Q]) {
break;
}
@ -55,15 +52,16 @@ int main() {
f.offset.y += 16;
}
struct int2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON(SDL_BUTTON_LEFT)) {
struct float2 mouse;
if (SDL_GetMouseState(&mouse.x, &mouse.y) & SDL_BUTTON_LMASK) {
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 };
mouse_prev = (struct float2) { -1, -1 };
}
Field_scroll(&f, rend);
@ -71,7 +69,7 @@ int main() {
char buf[72];
SDL_SetRenderDrawColor(rend, 0, 0, 0, 255);
SDL_RenderFillRect(rend, &(SDL_Rect) {
SDL_RenderFillRect(rend, &(SDL_FRect) {
0, 0, sizeof(buf)*font_w, 8*font_h
});
@ -93,7 +91,7 @@ int main() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
if (e.type == SDL_EVENT_QUIT) {
running = 0;
}
}