Init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/cmake-build-*/
|
||||||
|
/.idea
|
18
CMakeLists.txt
Normal file
18
CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
cmake_minimum_required(VERSION 4.0)
|
||||||
|
project(factorygame C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 23)
|
||||||
|
|
||||||
|
find_package(PkgConfig REQUIRED)
|
||||||
|
pkg_check_modules(SDL2 REQUIRED sdl2)
|
||||||
|
|
||||||
|
|
||||||
|
add_executable(factorygame
|
||||||
|
util/font.c
|
||||||
|
util/font.h
|
||||||
|
util/audio.c
|
||||||
|
util/audio.h
|
||||||
|
main.c) # Ensure the target is defined before linking
|
||||||
|
|
||||||
|
target_link_libraries(factorygame SDL2 SDL2_ttf SDL2_image SDL2_gfx SDL2_mixer SDL2_net m)
|
BIN
assets/dopravnik.png
Normal file
BIN
assets/dopravnik.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 187 B |
369
main.c
Normal file
369
main.c
Normal file
@@ -0,0 +1,369 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "util/font.h"
|
||||||
|
#include "util/audio.h"
|
||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
|
||||||
|
#define MAP_WIDTH 64
|
||||||
|
#define MAP_HEIGHT 36
|
||||||
|
|
||||||
|
#define TILE_SIZE 16
|
||||||
|
|
||||||
|
#define DISPLAY_WIDTH MAP_WIDTH * TILE_SIZE
|
||||||
|
#define DISPLAY_HEIGHT MAP_HEIGHT * TILE_SIZE
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BELT_LEFT_DOWN,
|
||||||
|
BELT_LEFT,
|
||||||
|
BELT_LEFT_UP,
|
||||||
|
BELT_UP,
|
||||||
|
BELT_RIGHT_UP,
|
||||||
|
BELT_RIGHT,
|
||||||
|
BELT_RIGHT_DOWN,
|
||||||
|
BELT_DOWN
|
||||||
|
} BeltDirection;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float x, y; // local position in tile (0.0–1.0)
|
||||||
|
bool active;
|
||||||
|
} Item;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool hasBelt;
|
||||||
|
BeltDirection direction;
|
||||||
|
int frameOffset;
|
||||||
|
Item item;
|
||||||
|
} Tile;
|
||||||
|
|
||||||
|
Tile tileMap[MAP_HEIGHT][MAP_WIDTH];
|
||||||
|
|
||||||
|
//Screen dimension constants
|
||||||
|
const int SCREEN_WIDTH = DISPLAY_WIDTH;
|
||||||
|
const int SCREEN_HEIGHT = DISPLAY_HEIGHT;
|
||||||
|
const int targetFPS = 60;
|
||||||
|
const int delayNeeded = 1000 / targetFPS;
|
||||||
|
|
||||||
|
//The window we'll be rendering to
|
||||||
|
SDL_Window *window = NULL;
|
||||||
|
volatile bool running = true;
|
||||||
|
|
||||||
|
//The surface contained by the window
|
||||||
|
SDL_Renderer *renderer = NULL;
|
||||||
|
|
||||||
|
#define biggerFont fonts[0]
|
||||||
|
#define smallFont fonts[1]
|
||||||
|
#define smallerFont fonts[2]
|
||||||
|
|
||||||
|
#define fontCount 3
|
||||||
|
|
||||||
|
BitmapFont fonts[fontCount];
|
||||||
|
|
||||||
|
unsigned long frames = 0;
|
||||||
|
bool cursor = true;
|
||||||
|
|
||||||
|
SDL_Texture *beltTex;
|
||||||
|
|
||||||
|
void msleep(unsigned int milliseconds) {
|
||||||
|
struct timespec ts;
|
||||||
|
ts.tv_sec = milliseconds / 1000;
|
||||||
|
ts.tv_nsec = (milliseconds % 1000) * 1000000;
|
||||||
|
nanosleep(&ts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateTestMap() {
|
||||||
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
||||||
|
tileMap[y][x] = (Tile){0};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int x = 5; x < 5 + (4*2); x+=2) {
|
||||||
|
tileMap[10][x].hasBelt = true;
|
||||||
|
tileMap[10][x].frameOffset = 0;
|
||||||
|
}
|
||||||
|
tileMap[10][5].direction = BELT_LEFT;
|
||||||
|
tileMap[10][7].direction = BELT_RIGHT;
|
||||||
|
tileMap[10][9].direction = BELT_UP;
|
||||||
|
tileMap[10][11].direction = BELT_DOWN;
|
||||||
|
|
||||||
|
tileMap[10][5].item = (Item){.x = 0.5f, .y = 0.5f, .active = true};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int init() {
|
||||||
|
//Initialize SDL
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, NULL);
|
||||||
|
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||||
|
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Initialize SDL_ttf
|
||||||
|
if (TTF_Init() == -1) {
|
||||||
|
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create window
|
||||||
|
window = SDL_CreateWindow("Factory game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||||
|
SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
||||||
|
if (window == NULL) {
|
||||||
|
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//Get window surface
|
||||||
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
if (renderer == NULL) {
|
||||||
|
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create OpenGL context
|
||||||
|
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
||||||
|
if (!glContext) {
|
||||||
|
fprintf(stderr, "SDL_GL_CreateContext failed: %s\n", SDL_GetError());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use OpenGL context
|
||||||
|
SDL_GL_MakeCurrent(window, glContext); // Make sure OpenGL context is current before any OpenGL rendering
|
||||||
|
|
||||||
|
SDL_AudioSpec spec = {0};
|
||||||
|
spec.freq = SAMPLE_RATE;
|
||||||
|
spec.format = AUDIO_F32SYS;
|
||||||
|
spec.channels = 1;
|
||||||
|
spec.samples = 4096;
|
||||||
|
spec.callback = audio_callback;
|
||||||
|
spec.userdata = &audioData;
|
||||||
|
|
||||||
|
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
|
||||||
|
if (dev == 0) {
|
||||||
|
printf("Failed to open audio: %s\n", SDL_GetError());
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_PauseAudioDevice(dev, 0);
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_SetRenderTarget(renderer, NULL);
|
||||||
|
|
||||||
|
SDL_Rect viewport = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||||
|
SDL_RenderSetViewport(renderer, &viewport);
|
||||||
|
|
||||||
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||||
|
biggerFont = prepText(renderer, 16, "PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
|
smallFont = prepText(renderer, 12, "PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
|
smallerFont = prepText(renderer, 8, "PublicPixel.ttf", 255, 255, 255, 255);
|
||||||
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
|
|
||||||
|
beltTex = IMG_LoadTexture(renderer, "../assets/dopravnik.png");
|
||||||
|
|
||||||
|
generateTestMap();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void updateItems() {
|
||||||
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
||||||
|
Tile *t = &tileMap[y][x];
|
||||||
|
if (!t->hasBelt || !t->item.active) continue;
|
||||||
|
|
||||||
|
float speed = 0.05f;
|
||||||
|
switch (t->direction) {
|
||||||
|
case BELT_LEFT: t->item.x -= speed; break;
|
||||||
|
case BELT_RIGHT: t->item.x += speed; break;
|
||||||
|
case BELT_UP: t->item.y -= speed; break;
|
||||||
|
case BELT_DOWN: t->item.y += speed; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t->item.x < 0 || t->item.x > 1 || t->item.y < 0 || t->item.y > 1) {
|
||||||
|
int nx = x, ny = y;
|
||||||
|
if (t->direction == BELT_LEFT) nx--;
|
||||||
|
if (t->direction == BELT_RIGHT) nx++;
|
||||||
|
if (t->direction == BELT_UP) ny--;
|
||||||
|
if (t->direction == BELT_DOWN) ny++;
|
||||||
|
if (nx >= 0 && nx < MAP_WIDTH && ny >= 0 && ny < MAP_HEIGHT) {
|
||||||
|
Tile *next = &tileMap[ny][nx];
|
||||||
|
if (next->hasBelt && next->direction == t->direction && !next->item.active) {
|
||||||
|
next->item = (Item){ .x = 0.5f, .y = 0.5f, .active = true };
|
||||||
|
t->item.active = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t->item.active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void renderBelt(SDL_Texture *tex, int x, int y, int w, int h, BeltDirection dir) {
|
||||||
|
int texW, texH;
|
||||||
|
SDL_QueryTexture(tex, NULL, NULL, &texW, &texH);
|
||||||
|
|
||||||
|
static int scrollFrame = 0;
|
||||||
|
int scrollSpeed = 1; // pixels per step
|
||||||
|
int scrollDelay = 15; // frames between steps
|
||||||
|
|
||||||
|
if (frames % scrollDelay == 0) {
|
||||||
|
scrollFrame += scrollSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Rect src1, src2, dst1, dst2;
|
||||||
|
|
||||||
|
if (dir == BELT_LEFT || dir == BELT_RIGHT) {
|
||||||
|
int offset = scrollFrame % texW;
|
||||||
|
|
||||||
|
if (dir == BELT_LEFT) {
|
||||||
|
offset = texW - offset; // reverse scroll
|
||||||
|
}
|
||||||
|
|
||||||
|
src1 = (SDL_Rect){offset, 0, texW - offset, texH};
|
||||||
|
dst1 = (SDL_Rect){x, y, w - offset, h};
|
||||||
|
|
||||||
|
src2 = (SDL_Rect){0, 0, offset, texH};
|
||||||
|
dst2 = (SDL_Rect){x + (w - offset), y, offset, h};
|
||||||
|
|
||||||
|
SDL_RenderCopy(renderer, tex, &src1, &dst1);
|
||||||
|
SDL_RenderCopy(renderer, tex, &src2, &dst2);
|
||||||
|
} else {
|
||||||
|
int offset = scrollFrame % texH;
|
||||||
|
|
||||||
|
if (dir == BELT_UP) {
|
||||||
|
offset = texH - offset; // reverse scroll
|
||||||
|
}
|
||||||
|
|
||||||
|
src1 = (SDL_Rect){0, offset, texW, texH - offset};
|
||||||
|
dst1 = (SDL_Rect){x, y, w, h - offset};
|
||||||
|
|
||||||
|
src2 = (SDL_Rect){0, 0, texW, offset};
|
||||||
|
dst2 = (SDL_Rect){x, y + (h - offset), w, offset};
|
||||||
|
|
||||||
|
SDL_RenderCopy(renderer, tex, &src1, &dst1);
|
||||||
|
SDL_RenderCopy(renderer, tex, &src2, &dst2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void renderAllBelts() {
|
||||||
|
int tileSize = TILE_SIZE;
|
||||||
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
||||||
|
Tile t = tileMap[y][x];
|
||||||
|
if (!t.hasBelt) continue;
|
||||||
|
int px = x * tileSize;
|
||||||
|
int py = y * tileSize;
|
||||||
|
renderBelt(beltTex, px, py, tileSize, tileSize, t.direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int render() {
|
||||||
|
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
|
SDL_Rect rect2;
|
||||||
|
rect2.x = 20;
|
||||||
|
rect2.y = 20;
|
||||||
|
rect2.w = 0;
|
||||||
|
rect2.h = 0;
|
||||||
|
|
||||||
|
renderAllBelts();
|
||||||
|
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
frames++;
|
||||||
|
if (!(frames % 60)) {
|
||||||
|
cursor = !cursor;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int processEvent(SDL_Event e) {
|
||||||
|
if (e.type == SDL_QUIT) { return 0; }
|
||||||
|
else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||||
|
int newWidth = e.window.data1;
|
||||||
|
int newHeight = e.window.data2;
|
||||||
|
|
||||||
|
// Adjust the viewport to match the new window size;
|
||||||
|
SDL_Rect viewport = {0, 0, newWidth, newHeight};
|
||||||
|
SDL_RenderSetViewport(renderer, &viewport);
|
||||||
|
} else if (e.type == SDL_KEYDOWN) {
|
||||||
|
int keySym = e.key.keysym.sym;
|
||||||
|
int keyMod = e.key.keysym.mod;
|
||||||
|
cursor = true;
|
||||||
|
switch (keySym) {
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
|
||||||
|
SDL_Rect viewport;
|
||||||
|
SDL_RenderGetViewport(renderer, &viewport);
|
||||||
|
|
||||||
|
SDL_Rect mouset;
|
||||||
|
mouset.w = 1;
|
||||||
|
mouset.h = 1;
|
||||||
|
SDL_GetMouseState(&mouset.x, &mouset.y);
|
||||||
|
// Translate mouse coordinates to viewport space
|
||||||
|
SDL_Rect mouse;
|
||||||
|
mouse.w = 1;
|
||||||
|
mouse.h = 1;
|
||||||
|
mouse.x = ((mouset.x - viewport.x) * SCREEN_WIDTH) / viewport.w;
|
||||||
|
mouse.y = (mouset.y - viewport.y) * SCREEN_HEIGHT / viewport.h;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[]) {
|
||||||
|
int status = init();
|
||||||
|
if (status) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Hack to get window to stay up
|
||||||
|
SDL_Event e;
|
||||||
|
Uint64 start;
|
||||||
|
Uint64 end;
|
||||||
|
while (running) {
|
||||||
|
start = SDL_GetTicks64();
|
||||||
|
|
||||||
|
while (SDL_PollEvent(&e)) {
|
||||||
|
running = processEvent(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateItems();
|
||||||
|
status = render();
|
||||||
|
if (status) {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
end = SDL_GetTicks64();
|
||||||
|
const unsigned long timeNeeded = end - start;
|
||||||
|
if (timeNeeded < delayNeeded) {
|
||||||
|
SDL_Delay(delayNeeded - timeNeeded);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < fontCount; i++) {
|
||||||
|
destroyFont(&fonts[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
puts(SDL_GetError());
|
||||||
|
if (renderer) SDL_DestroyRenderer(renderer);
|
||||||
|
if (window) SDL_DestroyWindow(window);
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
50
util/audio.c
Normal file
50
util/audio.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
// Created by bruno on 16.2.2025.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "audio.h"
|
||||||
|
|
||||||
|
AudioData audioData;
|
||||||
|
|
||||||
|
void audio_callback(void *userdata, Uint8 *stream, int len) {
|
||||||
|
AudioData *audio = (AudioData *) userdata;
|
||||||
|
int samples = len / sizeof(float);
|
||||||
|
|
||||||
|
for (int i = 0; i < samples; i++) {
|
||||||
|
float mix = 0.0f;
|
||||||
|
int activeVoices = 0;
|
||||||
|
|
||||||
|
for (int v = 0; v < NUM_SYNTH_VOICES; v++) {
|
||||||
|
SynthVoice *voice = &audio->synthVoices[v];
|
||||||
|
if (voice->volume == 0 || voice->frequency == 0) continue;
|
||||||
|
|
||||||
|
float sample = 0.0f;
|
||||||
|
float t = (float) voice->phase / 255.0f * 2.0f - 1.0f;
|
||||||
|
|
||||||
|
switch (voice->waveform) {
|
||||||
|
default:
|
||||||
|
case WAVE_SINE:
|
||||||
|
sample = sinf(voice->phase * 2.0f * M_PI / 256.0f);
|
||||||
|
break;
|
||||||
|
case WAVE_SQUARE:
|
||||||
|
sample = (t >= 0.0f) ? 1.0f : -1.0f;
|
||||||
|
break;
|
||||||
|
case WAVE_SAWTOOTH:
|
||||||
|
sample = t;
|
||||||
|
break;
|
||||||
|
case WAVE_TRIANGLE:
|
||||||
|
sample = (t < 0) ? -t : t;
|
||||||
|
break;
|
||||||
|
case WAVE_NOISE:
|
||||||
|
sample = ((float) rand() / RAND_MAX) * 2.0f - 1.0f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
voice->phase += (uint8_t) ((voice->frequency * 256) / SAMPLE_RATE);
|
||||||
|
mix += sample * (voice->volume / 255.0f);
|
||||||
|
activeVoices++;
|
||||||
|
}
|
||||||
|
|
||||||
|
((float *) stream)[i] = (activeVoices > 0) ? mix / activeVoices : 0.0f;
|
||||||
|
}
|
||||||
|
}
|
39
util/audio.h
Normal file
39
util/audio.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
// Created by bruno on 16.2.2025.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RISCB_AUDIO_H
|
||||||
|
#define RISCB_AUDIO_H
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define SAMPLE_RATE 44100
|
||||||
|
#define NUM_SYNTH_VOICES 3
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
WAVE_SINE,
|
||||||
|
WAVE_SQUARE,
|
||||||
|
WAVE_SAWTOOTH,
|
||||||
|
WAVE_TRIANGLE,
|
||||||
|
WAVE_NOISE
|
||||||
|
} Waveform;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t volume;
|
||||||
|
uint16_t frequency;
|
||||||
|
uint8_t phase;
|
||||||
|
Waveform waveform;
|
||||||
|
} SynthVoice;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SynthVoice synthVoices[NUM_SYNTH_VOICES];
|
||||||
|
} AudioData;
|
||||||
|
|
||||||
|
extern AudioData audioData;
|
||||||
|
|
||||||
|
void audio_callback(void *userdata, Uint8 *stream, int len);
|
||||||
|
|
||||||
|
#endif //RISCB_AUDIO_H
|
61
util/font.c
Normal file
61
util/font.c
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// Created by bruno on 1.2.2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "font.h"
|
||||||
|
|
||||||
|
BitmapFont
|
||||||
|
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||||
|
TTF_Font *gFont = TTF_OpenFont(file, pxSize);
|
||||||
|
BitmapFont out;
|
||||||
|
out.size = pxSize;
|
||||||
|
out.color = (SDL_Color) {r, g, b, a};
|
||||||
|
unsigned int i = 1;
|
||||||
|
do {
|
||||||
|
if (i == 173) { //specifically this char is 0 width (IDK why)
|
||||||
|
out.surface[i] = SDL_CreateRGBSurface(0, pxSize, pxSize, 32, 0, 0, 0, 0);
|
||||||
|
} else {
|
||||||
|
char tmpOut[2] = {i, 0};
|
||||||
|
out.surface[i] = TTF_RenderText_Solid(gFont, tmpOut, out.color);
|
||||||
|
}
|
||||||
|
out.texture[i] = SDL_CreateTextureFromSurface(renderer, out.surface[i]);
|
||||||
|
i++;
|
||||||
|
} while (i < 256);
|
||||||
|
|
||||||
|
TTF_CloseFont(gFont);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderText(SDL_Renderer *renderer, BitmapFont font, char *string, uint16_t x, uint16_t y) {
|
||||||
|
SDL_Rect charRect;
|
||||||
|
charRect.x = 0;
|
||||||
|
charRect.y = 0;
|
||||||
|
charRect.w = font.size;
|
||||||
|
charRect.h = font.size;
|
||||||
|
SDL_Rect outRect = charRect;
|
||||||
|
outRect.x = x;
|
||||||
|
outRect.y = y;
|
||||||
|
|
||||||
|
while (*string) {
|
||||||
|
if (*string == '\n') {
|
||||||
|
outRect.x = x;
|
||||||
|
outRect.y += charRect.h + 4;
|
||||||
|
string++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
SDL_RenderCopy(renderer, font.texture[*string], &charRect, &outRect);
|
||||||
|
outRect.x += charRect.w + 1;
|
||||||
|
string++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroyFont(BitmapFont *font) {
|
||||||
|
for (uint16_t i = 1; i < 256; i++) {
|
||||||
|
if (font->texture[i]) {
|
||||||
|
SDL_DestroyTexture(font->texture[i]);
|
||||||
|
}
|
||||||
|
if (font->surface[i]) {
|
||||||
|
SDL_FreeSurface(font->surface[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
util/font.h
Normal file
26
util/font.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Created by bruno on 1.2.2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef RISCB_FONT_H
|
||||||
|
#define RISCB_FONT_H
|
||||||
|
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SDL_Texture *texture[256];
|
||||||
|
SDL_Surface *surface[256];
|
||||||
|
uint8_t size;
|
||||||
|
SDL_Color color;
|
||||||
|
} BitmapFont;
|
||||||
|
|
||||||
|
BitmapFont
|
||||||
|
prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
|
||||||
|
|
||||||
|
void destroyFont(BitmapFont *font);
|
||||||
|
|
||||||
|
void renderText(SDL_Renderer *renderer, BitmapFont font, char *string, uint16_t x, uint16_t y);
|
||||||
|
|
||||||
|
#endif //RISCB_FONT_H
|
Reference in New Issue
Block a user