Created
October 25, 2014 16:44
-
-
Save TomMinor/855879407c5acca83225 to your computer and use it in GitHub Desktop.
C SDL2-ttf ( modified from LazyFoo )
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
/*This source code copyrighted by Lazy Foo' Productions (2004-2014) | |
and may not be redistributed without written permission.*/ | |
//Using SDL, SDL_image, SDL_ttf, standard IO, math, and strings | |
#include <SDL.h> | |
#include <SDL_image.h> | |
#include <SDL_ttf.h> | |
#include <stdio.h> | |
#include <math.h> | |
//Screen dimension constants | |
const int SCREEN_WIDTH = 640; | |
const int SCREEN_HEIGHT = 480; | |
//Starts up SDL and creates window | |
bool init(); | |
//Loads media | |
bool loadMedia(); | |
//Frees media and shuts down SDL | |
void close(); | |
//The window we'll be rendering to | |
SDL_Window* gWindow = NULL; | |
//The window renderer | |
SDL_Renderer* gRenderer = NULL; | |
//Globally used font | |
TTF_Font *gFont = NULL; | |
//The actual hardware texture | |
SDL_Texture* font_texture = NULL; | |
//Image dimensions | |
int mWidth = 0; | |
int mHeight = 0; | |
SDL_Texture* loadTexture( const char* path ) | |
{ | |
//The final texture | |
SDL_Texture* newTexture = NULL; | |
//Load image at specified path | |
SDL_Surface* loadedSurface = IMG_Load( path ); | |
if( loadedSurface == NULL ) | |
{ | |
printf( "Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError() ); | |
return NULL; | |
} | |
//Color key image | |
SDL_SetColorKey( loadedSurface, SDL_TRUE, SDL_MapRGB( loadedSurface->format, 0, 0xFF, 0xFF ) ); | |
//Create texture from surface pixels | |
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface ); | |
if( newTexture == NULL ) | |
{ | |
printf( "Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError() ); | |
return NULL; | |
} | |
//Get image dimensions | |
mWidth = loadedSurface->w; | |
mHeight = loadedSurface->h; | |
//Get rid of old loaded surface | |
SDL_FreeSurface( loadedSurface ); | |
//Return success | |
return newTexture; | |
} | |
void render( int x, int y) | |
{ | |
// C lacks default params, lazy workaround | |
SDL_Rect* clip=NULL; | |
double angle = 0.0; | |
SDL_Point* center = NULL; | |
SDL_RendererFlip flip = SDL_FLIP_NONE; | |
//Set rendering space and render to screen | |
SDL_Rect renderQuad = { x, y, mWidth, mHeight }; | |
//Set clip rendering dimensions | |
if( clip != NULL ) | |
{ | |
renderQuad.w = clip->w; | |
renderQuad.h = clip->h; | |
} | |
//Render to screen | |
SDL_RenderCopyEx( gRenderer, font_texture, clip, &renderQuad, angle, center, flip ); | |
} | |
bool loadFromRenderedText( const char* textureText, SDL_Color textColor ) | |
{ | |
//Render text surface | |
SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText, textColor ); | |
if( textSurface == NULL ) | |
{ | |
printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() ); | |
return false; | |
} | |
//Create texture from surface pixels | |
font_texture = SDL_CreateTextureFromSurface( gRenderer, textSurface ); | |
if( font_texture == NULL ) | |
{ | |
printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() ); | |
return false; | |
} | |
//Get image dimensions | |
mWidth = textSurface->w; | |
mHeight = textSurface->h; | |
//Get rid of old surface | |
SDL_FreeSurface( textSurface ); | |
//Return success | |
return true; | |
} | |
bool init() | |
{ | |
//Initialization flag | |
bool success = true; | |
//Initialize SDL | |
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) | |
{ | |
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); | |
return false; | |
} | |
//Set texture filtering to linear | |
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) | |
{ | |
printf( "Warning: Linear texture filtering not enabled!" ); | |
} | |
//Create window | |
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); | |
if( gWindow == NULL ) | |
{ | |
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() ); | |
return false; | |
} | |
//Create vsynced renderer for window | |
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ); | |
if( gRenderer == NULL ) | |
{ | |
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() ); | |
return false; | |
} | |
//Initialize renderer color | |
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); | |
//Initialize PNG loading | |
int imgFlags = IMG_INIT_PNG; | |
if( !( IMG_Init( imgFlags ) & imgFlags ) ) | |
{ | |
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() ); | |
return false; | |
} | |
//Initialize SDL_ttf | |
if( TTF_Init() == -1 ) | |
{ | |
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() ); | |
return false; | |
} | |
return true; | |
} | |
bool loadMedia() | |
{ | |
//Loading success flag | |
bool success = true; | |
//Open the font | |
gFont = TTF_OpenFont( "lazy.ttf", 28 ); | |
if( gFont == NULL ) | |
{ | |
printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() ); | |
return false; | |
} | |
//Render text | |
SDL_Color textColor = { 0, 0, 0 }; | |
if( !loadFromRenderedText( "The quick brown fox jumps over the lazy dog", textColor ) ) | |
{ | |
printf( "Failed to render text texture!\n" ); | |
return false; | |
} | |
return true; | |
} | |
void close() | |
{ | |
//Free loaded images | |
free(font_texture); | |
//Free global font | |
TTF_CloseFont( gFont ); | |
gFont = NULL; | |
//Destroy window | |
SDL_DestroyRenderer( gRenderer ); | |
SDL_DestroyWindow( gWindow ); | |
gWindow = NULL; | |
gRenderer = NULL; | |
//Quit SDL subsystems | |
TTF_Quit(); | |
IMG_Quit(); | |
SDL_Quit(); | |
} | |
int main( int argc, char* args[] ) | |
{ | |
//Start up SDL and create window | |
if( !init() ) | |
{ | |
printf( "Failed to initialize!\n" ); | |
return 1; | |
} | |
//Load media | |
if( !loadMedia() ) | |
{ | |
printf( "Failed to load media!\n" ); | |
return 1; | |
} | |
//Main loop flag | |
bool quit = false; | |
//Event handler | |
SDL_Event e; | |
//While application is running | |
while( !quit ) | |
{ | |
//Handle events on queue | |
while( SDL_PollEvent( &e ) != 0 ) | |
{ | |
//User requests quit | |
if( e.type == SDL_QUIT ) | |
{ | |
quit = true; | |
} | |
} | |
//Clear screen | |
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF ); | |
SDL_RenderClear( gRenderer ); | |
//Render current frame | |
render( ( SCREEN_WIDTH - mWidth ) / 2, ( SCREEN_HEIGHT - mHeight ) / 2 ); | |
//Update screen | |
SDL_RenderPresent( gRenderer ); | |
} | |
//Free resources and close SDL | |
close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment