Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL2/SDL.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <SDL2/SDL_ttf.h>
- #define WINDOW_WIDTH 600
- #define WINDOW_HEIGHT 650
- #define FONT_COORDINATES "fonts/Hack-Regular.ttf"
- #define FONT_HEIGHT 20
- void panic(const char* format, ...) {
- va_list va;
- va_start(va, format);
- vfprintf(stderr, format, va);
- fprintf(stderr, "\n");
- va_end(va);
- exit(1);
- }
- typedef struct {
- SDL_Window* window;
- SDL_Renderer* r;
- struct {
- int width;
- int height;
- } windowDimensions;
- struct {
- SDL_Texture* coordinates_horizontal;
- } textures;
- int tileSize;
- } RenderContext;
- SDL_Texture* generateHorizontalCoordinates(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to generateHorizontalCoordinates()");
- }
- // load font
- TTF_Font* font = TTF_OpenFont(FONT_COORDINATES, 10);
- if (font == NULL) {
- panic("Could not open font");
- }
- // 1. calculate texture dimensions (board width and font height)
- // and load font in correct size
- const int fontSize = FONT_HEIGHT;
- int ret = TTF_SetFontSize(font, fontSize);
- if (ret == -1) {
- panic("Could not set font size");
- }
- // 2. allocate master texture
- SDL_Texture* masterTexture = SDL_CreateTexture(
- rc->r,
- SDL_PIXELFORMAT_RGBA32,
- SDL_TEXTUREACCESS_TARGET,
- rc->windowDimensions.width,
- FONT_HEIGHT
- );
- if (masterTexture == NULL) {
- panic("Could not create texture");
- }
- SDL_SetTextureBlendMode(masterTexture, SDL_BLENDMODE_BLEND);
- // 3. generate temporary textures for each letter and copy it to "master texture"
- //TODO FIXME sometimes some letters are not rendered at all!
- for (int i = 0; i < 9; i++) {
- char text[2] = {0};
- text[0] = '1' + i;
- SDL_Color color = {0, 0, 0, 255};
- SDL_Surface* letterSurface = TTF_RenderUTF8_Solid(font, text, color);
- if (letterSurface == NULL) {
- panic("Could not create letterSurface");
- }
- SDL_Texture* letterTexture = SDL_CreateTextureFromSurface(rc->r, letterSurface);
- if (letterTexture == NULL) {
- panic("Could not create letterTexture");
- }
- // NB: We'll keep the surface around for its metadata
- // render to master texture
- int res = SDL_SetRenderTarget(rc->r, masterTexture);
- if (res != 0) {
- panic("Could not set render target");
- }
- SDL_Rect dst = {
- .w = letterSurface->w,
- .h = letterSurface->h,
- .x = (rc->tileSize / 2) + (i * rc->tileSize)
- - (letterSurface->w / 2),
- .y = 0
- };
- printf("i: %d, w: %d, h: %d, x: %d, y: %d\n", i, dst.w, dst.h, dst.x, dst.y); //XXX
- SDL_RenderCopy(rc->r, letterTexture, NULL, &dst);
- // clean-up
- SDL_SetRenderTarget(rc->r, NULL);
- SDL_FreeSurface(letterSurface);
- SDL_DestroyTexture(letterTexture);
- }
- // 4. free font and return master texture (free in rc_free())
- TTF_CloseFont(font);
- return masterTexture;
- }
- void renderContext_init(RenderContext* rc, int boardSize) {
- if (rc == NULL) {
- panic("Passed NULL to rendercontext_init()");
- }
- rc->window = SDL_CreateWindow(
- "Goban",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- WINDOW_WIDTH,
- WINDOW_HEIGHT,
- 0
- );
- if (rc->window == NULL) {
- panic("Could not init Window: %s", SDL_GetError());
- }
- rc->r = SDL_CreateRenderer(
- rc->window,
- -1,
- SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE
- );
- if (rc->r == NULL) {
- panic("Could not init Renderer: %s", SDL_GetError());
- }
- // init SDL_TTF
- TTF_Init();
- rc->windowDimensions.width = WINDOW_WIDTH;
- rc->windowDimensions.height = WINDOW_HEIGHT;
- rc->tileSize = rc->windowDimensions.width / boardSize;
- rc->textures.coordinates_horizontal = generateHorizontalCoordinates(rc);
- }
- void renderContext_free(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to rendercontext_free()");
- }
- TTF_Quit();
- SDL_DestroyTexture(rc->textures.coordinates_horizontal);
- SDL_DestroyRenderer(rc->r);
- SDL_DestroyWindow(rc->window);
- }
- void renderBackground(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to renderBackground()");
- }
- SDL_SetRenderDrawColor(rc->r, 255, 255, 255, 255);
- SDL_Rect rect = {
- .w = rc->windowDimensions.width,
- .h = rc->windowDimensions.width,
- .x = 0,
- .y = 0
- };
- SDL_RenderFillRect(rc->r, &rect);
- }
- void renderCoordinates(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to renderCoordinates()");
- }
- SDL_Rect dst = {0};
- dst.w = rc->windowDimensions.width;
- dst.h = FONT_HEIGHT;
- SDL_RenderCopy(rc->r, rc->textures.coordinates_horizontal, NULL, &dst);
- }
- void renderBoard(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to renderBoard()");
- }
- renderBackground(rc);
- renderCoordinates(rc);
- }
- void renderContext_render(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to rendercontext_render()");
- }
- SDL_SetRenderDrawColor(rc->r, 255, 255, 255, 255);
- SDL_RenderClear(rc->r);
- renderBoard(rc);
- SDL_RenderPresent(rc->r);
- }
- bool shouldQuit = false;
- void handleEvents(RenderContext* rc) {
- if (rc == NULL) {
- panic("Passed NULL to handleEvents()");
- }
- SDL_Event e;
- while (SDL_PollEvent(&e)) {
- switch (e.type) {
- case SDL_QUIT:
- shouldQuit = true;
- break;
- }
- }
- }
- int main(void) {
- int ret;
- ret = SDL_Init(SDL_INIT_VIDEO);
- if (ret != 0) {
- panic("Could not init SDL: %s", SDL_GetError());
- }
- RenderContext rc;
- renderContext_init(&rc, 9);
- while (!shouldQuit) {
- handleEvents(&rc);
- renderContext_render(&rc);
- }
- renderContext_free(&rc);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement