forked from mobius3/kiwi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding renderdriver header and SDL2 implementation of it
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef RENDERDRIVER_H | ||
#define RENDERDRIVER_H | ||
|
||
typedef void * KW_Texture; | ||
typedef void * KW_Font; | ||
|
||
typedef struct KW_Rect { | ||
unsigned int x; | ||
unsigned int y; | ||
unsigned int w; | ||
unsigned int h; | ||
} KW_Rect; | ||
|
||
typedef struct KW_Color { | ||
unsigned char r; | ||
unsigned char g; | ||
unsigned char b; | ||
unsigned char a; | ||
} KW_Color; | ||
|
||
typedef struct KW_RenderDriver KW_RenderDriver; | ||
|
||
typedef enum KW_RenderDriver_TextStyle { | ||
KW_TTF_STYLE_NORMAL = 0x00, | ||
KW_TTF_STYLE_BOLD = 0x01, | ||
KW_TTF_STYLE_ITALIC = 0x02, | ||
KW_TTF_STYLE_UNDERLINE = 0x04, | ||
KW_TTF_STYLE_STRIKETHROUGH = 0x08 | ||
} KW_RenderDriver_TextStyle; | ||
|
||
struct KW_RenderDriver { | ||
void (*renderCopy)(KW_RenderDriver * driver, KW_Texture src, const KW_Rect * clip, const KW_Rect * dstRect); | ||
KW_Texture (*renderText)(KW_RenderDriver * driver, const KW_Font font, const char * text, KW_Color color, KW_RenderDriver_TextStyle style); | ||
KW_Font (*loadFont)(KW_RenderDriver * driver, const char * fontFile, unsigned ptSize); | ||
KW_Texture (*loadTexture)(KW_RenderDriver * driver, const char * textureFile); | ||
void (*releaseTexture)(KW_RenderDriver * driver, KW_Texture texture); | ||
void (*releaseFont)(KW_RenderDriver * driver, KW_Font font); | ||
void * priv; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "KW_renderdriver_sdl2.h" | ||
#include "KW_renderdriver.h" | ||
#include "SDL.h" | ||
#include "SDL_ttf.h" | ||
#include "SDL_image.h" | ||
|
||
|
||
typedef struct KWSDL { | ||
SDL_Renderer * renderer; | ||
SDL_Window * window; | ||
} KWSDL; | ||
|
||
static KW_Font KWSDL_loadFont(KW_RenderDriver * driver, const char * font, unsigned ptSize); | ||
static KW_Texture KWSDL_loadTexture(KW_RenderDriver * driver, const char * texturefile); | ||
static KW_Texture KWSDL_renderText(KW_RenderDriver * driver, const KW_Font font, const char * text, KW_Color color, KW_RenderDriver_TextStyle style); | ||
static void KWSDL_releaseTexture(KW_RenderDriver * driver, KW_Texture texture); | ||
static void KWSDL_releaseFont(KW_RenderDriver * driver, KW_Font font); | ||
static void KWSDL_renderCopy(KW_RenderDriver * driver, KW_Texture texture, const KW_Rect * src, const KW_Rect * dst); | ||
|
||
struct KW_RenderDriver * KW_CreateSDL2RenderDriver(SDL_Renderer * renderer, SDL_Window * window){ | ||
struct KWSDL * kwsdl = calloc(sizeof(*kwsdl), 1); | ||
struct KW_RenderDriver * rd = calloc(sizeof(*rd), 1); | ||
|
||
kwsdl->renderer = renderer; | ||
kwsdl->window = window; | ||
|
||
rd->loadFont = KWSDL_loadFont; | ||
rd->loadTexture = KWSDL_loadTexture; | ||
rd->renderText = KWSDL_renderText; | ||
rd->releaseFont = KWSDL_releaseFont; | ||
rd->releaseTexture = KWSDL_releaseTexture; | ||
rd->renderCopy = KWSDL_renderCopy; | ||
|
||
rd->priv = kwsdl; | ||
return rd; | ||
} | ||
|
||
static KW_Font KWSDL_loadFont(KW_RenderDriver * driver, const char * font, unsigned ptSize) { | ||
TTF_Font * f = TTF_OpenFont(font, ptSize); | ||
(void)driver; | ||
if (f == NULL) { | ||
fprintf(stderr, "KW_RenderDriver_SDL: Could not load font %s: %s\n", font, TTF_GetError()); | ||
return NULL; | ||
} | ||
return f; | ||
} | ||
|
||
static KW_Texture KWSDL_loadTexture(KW_RenderDriver * driver, const char * texturefile) { | ||
KWSDL * kwsdl = (KWSDL *) driver->priv; | ||
SDL_Texture * t = IMG_LoadTexture(kwsdl->renderer, texturefile); | ||
if (t == NULL) { | ||
fprintf(stderr, "KW_RenderDriver_SDL: Could not load texture %s: %s\n", texturefile, IMG_GetError()); | ||
return NULL; | ||
} | ||
return t; | ||
} | ||
|
||
static KW_Texture KWSDL_renderText(KW_RenderDriver * driver, const KW_Font font, const char * text, KW_Color color, KW_RenderDriver_TextStyle style) { | ||
KWSDL * kwsdl = (KWSDL *) driver->priv; | ||
int previousstyle; | ||
SDL_Color sdlcolor; | ||
SDL_Surface * textsurface; | ||
SDL_Texture * ret; | ||
|
||
sdlcolor.r = color.r, sdlcolor.g = color.g, sdlcolor.b = color.b, sdlcolor.a = color.a; | ||
if (font == NULL || text == NULL) | ||
return NULL; | ||
|
||
previousstyle = TTF_GetFontStyle(font); | ||
TTF_SetFontStyle(font, style); | ||
textsurface = TTF_RenderUTF8_Blended(font, text, sdlcolor); | ||
ret = SDL_CreateTextureFromSurface(kwsdl->renderer, textsurface); | ||
SDL_FreeSurface(textsurface); | ||
TTF_SetFontStyle(font, previousstyle); | ||
return ret; | ||
} | ||
|
||
static void KWSDL_releaseTexture(KW_RenderDriver * driver, KW_Texture texture) { | ||
(void)driver; | ||
SDL_DestroyTexture(texture); | ||
} | ||
static void KWSDL_releaseFont(KW_RenderDriver * driver, KW_Font font) { | ||
(void)driver; | ||
TTF_CloseFont(font); | ||
} | ||
|
||
static void KWSDL_renderCopy(KW_RenderDriver * driver, KW_Texture texture, const KW_Rect * src, const KW_Rect * dst) { | ||
KWSDL * kwsdl = (KWSDL *) driver->priv; | ||
SDL_Rect srcRect, dstRect; | ||
srcRect.x = src->x; srcRect.y = src->y; srcRect.w = src->w; srcRect.h = src->h; | ||
dstRect.x = dst->x; dstRect.y = dst->y; dstRect.w = dst->w; dstRect.h = dst->h; | ||
SDL_RenderCopy(kwsdl->renderer, texture, &srcRect, &dstRect); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef KW_RENDERDRIVER_SDL2 | ||
#define KW_RENDERDRIVER_SDL2 | ||
|
||
#include "SDL.h" | ||
|
||
struct KW_RenderDriver * KW_CreateSDL2RenderDriver(SDL_Renderer * renderer, SDL_Window * window); | ||
|
||
#endif | ||
|