Commit 1b1270fa416eac940fef132d9c1c2aec32fcc6b0
1 parent
6d66dde7
Added ttf rendering support
Showing
5 changed files
with
47 additions
and
11 deletions
DejaVuSans.ttf
0 → 100644
No preview for this file type
makefile
primlib.c
| ... | ... | @@ -34,12 +34,13 @@ void gfx_line(int x1, int y1, int x2, int y2, enum color c) { |
| 34 | 34 | 255); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | -void gfx_filledTriangle(int x1, int y1, int x2, int y2, int x3, int y3, enum color c) { | |
| 37 | +void gfx_filledTriangle(int x1, int y1, int x2, int y2, int x3, int y3, | |
| 38 | + enum color c) { | |
| 38 | 39 | assert(c < COLOR_MAX); |
| 39 | 40 | Sint16 xtab[] = {x1, x2, x3}; |
| 40 | 41 | Sint16 ytab[] = {y1, y2, y3}; |
| 41 | - filledPolygonRGBA(renderer, xtab, ytab, 3, colors[c].r, colors[c].g, colors[c].b, | |
| 42 | - 255); | |
| 42 | + filledPolygonRGBA(renderer, xtab, ytab, 3, colors[c].r, colors[c].g, | |
| 43 | + colors[c].b, 255); | |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | void gfx_rect(int x1, int y1, int x2, int y2, enum color c) { |
| ... | ... | @@ -78,6 +79,28 @@ void gfx_textout(int x, int y, const char *s, enum color c) { |
| 78 | 79 | stringRGBA(renderer, x, y, s, colors[c].r, colors[c].g, colors[c].b, 255); |
| 79 | 80 | } |
| 80 | 81 | |
| 82 | +void gfx_ttfout(int x, int y, int size, const char *font_path, const char* s, enum color c) { | |
| 83 | + assert(c < COLOR_MAX); | |
| 84 | + TTF_Font *font = TTF_OpenFont(font_path, size); | |
| 85 | + if (!font) | |
| 86 | + fprintf(stderr, "Error opening font: %s\n", TTF_GetError()); | |
| 87 | + | |
| 88 | + SDL_Color color = {colors[c].r, colors[c].g, colors[c].b}; | |
| 89 | + SDL_Surface *surface = TTF_RenderText_Solid(font, s, color); | |
| 90 | + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); | |
| 91 | + | |
| 92 | + int texW = 0; | |
| 93 | + int texH = 0; | |
| 94 | + SDL_QueryTexture(texture, NULL, NULL, &texW, &texH); | |
| 95 | + SDL_Rect dstrect = {x, y, texW, texH}; | |
| 96 | + | |
| 97 | + SDL_RenderCopy(renderer, texture, NULL, &dstrect); | |
| 98 | + | |
| 99 | + SDL_DestroyTexture(texture); | |
| 100 | + SDL_FreeSurface(surface); | |
| 101 | + TTF_CloseFont(font); | |
| 102 | +} | |
| 103 | + | |
| 81 | 104 | int gfx_pollkey() { |
| 82 | 105 | SDL_Event event; |
| 83 | 106 | while (SDL_PollEvent(&event)) { |
| ... | ... | @@ -108,7 +131,7 @@ int gfx_isKeyDown(int key) { |
| 108 | 131 | int numkeys; |
| 109 | 132 | SDL_PumpEvents(); |
| 110 | 133 | SDL_Event event; |
| 111 | - while(SDL_PollEvent(&event)) { | |
| 134 | + while (SDL_PollEvent(&event)) { | |
| 112 | 135 | switch (event.type) { |
| 113 | 136 | case SDL_QUIT: |
| 114 | 137 | exit(3); |
| ... | ... | @@ -121,6 +144,8 @@ int gfx_isKeyDown(int key) { |
| 121 | 144 | } |
| 122 | 145 | |
| 123 | 146 | static void gfx_close() { |
| 147 | + TTF_Quit(); | |
| 148 | + | |
| 124 | 149 | SDL_DestroyRenderer(renderer); |
| 125 | 150 | SDL_DestroyWindow(window); |
| 126 | 151 | SDL_Quit(); |
| ... | ... | @@ -133,13 +158,20 @@ int gfx_init() { |
| 133 | 158 | fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 134 | 159 | return 1; |
| 135 | 160 | } |
| 161 | + | |
| 162 | + if (TTF_Init() < 0) { | |
| 163 | + fprintf(stderr, "Couldn't initialize TTF library: %s\n", TTF_GetError()); | |
| 164 | + return 1; | |
| 165 | + } | |
| 166 | + | |
| 136 | 167 | atexit(gfx_close); |
| 137 | 168 | |
| 138 | - window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, | |
| 139 | - SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, | |
| 140 | - SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
| 169 | + window = | |
| 170 | + SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
| 171 | + SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); | |
| 141 | 172 | if (window == NULL) { |
| 142 | - fprintf(stderr, "Window could not be created! SDL Error: %s\n", SDL_GetError()); | |
| 173 | + fprintf(stderr, "Window could not be created! SDL Error: %s\n", | |
| 174 | + SDL_GetError()); | |
| 143 | 175 | return 2; |
| 144 | 176 | } else { |
| 145 | 177 | // Create renderer for window |
| ... | ... | @@ -150,7 +182,8 @@ int gfx_init() { |
| 150 | 182 | } |
| 151 | 183 | |
| 152 | 184 | if (renderer == NULL) { |
| 153 | - fprintf(stderr,"Renderer could not be created! SDL Error: %s\n", SDL_GetError()); | |
| 185 | + fprintf(stderr, "Renderer could not be created! SDL Error: %s\n", | |
| 186 | + SDL_GetError()); | |
| 154 | 187 | return 3; |
| 155 | 188 | } |
| 156 | 189 | } | ... | ... |
primlib.h
| ... | ... | @@ -3,6 +3,8 @@ |
| 3 | 3 | |
| 4 | 4 | #include <SDL2/SDL.h> |
| 5 | 5 | #include <SDL2/SDL2_gfxPrimitives.h> |
| 6 | +#include <SDL2/SDL_ttf.h> | |
| 7 | + | |
| 6 | 8 | int gfx_init(); |
| 7 | 9 | enum color { BLACK, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, COLOR_MAX }; |
| 8 | 10 | void gfx_pixel(int x, int y, enum color c); |
| ... | ... | @@ -13,6 +15,7 @@ void gfx_filledRect(int x1, int y1, int x2, int y2, enum color c); |
| 13 | 15 | void gfx_filledCircle(int x, int y, int r, enum color c); |
| 14 | 16 | void gfx_rect(int x1, int y1, int x2, int y2, enum color c); |
| 15 | 17 | void gfx_textout(int x, int y, const char *s, enum color c); |
| 18 | +void gfx_ttfout(int x, int y, int size, const char *font_path, const char* s, enum color c); | |
| 16 | 19 | int gfx_screenWidth(); |
| 17 | 20 | int gfx_screenHeight(); |
| 18 | 21 | void gfx_updateScreen(); | ... | ... |
testlib.c
| ... | ... | @@ -8,7 +8,7 @@ int main(int argc, char *argv[]) { |
| 8 | 8 | for (int i = -99; i < 100; ++i) { |
| 9 | 9 | gfx_filledRect(0, 0, gfx_screenWidth() - 1, gfx_screenHeight() - 1, BLACK); |
| 10 | 10 | gfx_filledCircle(gfx_screenWidth() / 2 + i, gfx_screenHeight() / 2, 100, YELLOW); |
| 11 | - gfx_textout(gfx_screenWidth() / 2 - i, gfx_screenHeight() / 2, "This is a text", RED); | |
| 11 | + gfx_ttfout(gfx_screenWidth() / 2 - i, gfx_screenHeight() / 2, 40, "DejaVuSans.ttf", "This is a text", RED); | |
| 12 | 12 | gfx_updateScreen(); |
| 13 | 13 | SDL_Delay(10); |
| 14 | 14 | } | ... | ... |