nesemu/gui/char_map.c

54 lines
1.2 KiB
C
Raw Normal View History

2024-06-16 19:22:40 -04:00
//
// Created by william on 6/8/24.
//
#include <SDL.h>
#include <SDL_ttf.h>
#include "char_map.h"
SDL_Texture *map[CHAR_MAP_COUNT];
SDL_Texture **char_map_get(char c) {
int char_index = c - CHAR_MAP_MIN;
return &map[char_index];
}
void char_map_init(SDL_Renderer *renderer, TTF_Font *font) {
char buffer[2];
SDL_Color color = CHAR_MAP_COLOR;
SDL_Surface *surface;
SDL_Texture **texture;
for (int i = 0; i <= CHAR_MAP_COUNT; i++) {
buffer[0] = (char) (i + CHAR_MAP_MIN);
buffer[1] = '\0';
surface = TTF_RenderText_Solid(font, buffer, color);
texture = char_map_get(buffer[0]);
*texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
}
void char_map_uninit() {
for (int i = 0; i < CHAR_MAP_COUNT; i++) {
SDL_Texture *texture = map[i];
SDL_DestroyTexture(texture);
}
}
void char_map_render(SDL_Renderer *renderer, char *text) {
int x = 0;
for (int i = 0; i < strlen(text); i++) {
char c = text[i];
SDL_Texture *texture = *char_map_get(c);
SDL_Rect rect = {x, 0, 16, 16};
SDL_RenderCopy(renderer, texture, NULL, &rect);
x += 16;
}
}