-
-
Save robin-a-meade/927e4ced05b00d2ec2b74bee423cc3bb to your computer and use it in GitHub Desktop.
SDL2 Hello World | SDL2 Getting Started | SDL | OpenGL
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
// SDL2 Hello, World! | |
// This should display a white screen for 2 seconds | |
// with the text 'Hello World!' | |
// Prerequisites (Fedora): | |
// sudo dnf install SDL2-static | |
// sudo dnf install SDL2-devel (Actually, this gets installed as a dependency of SDL2-static) | |
// sudo dnf install SDL2_ttf-devel | |
// Compile: | |
// gcc -g sdl2hello.cc -o sdl2hello -lSDL2 -lSDL2main -lSDL2_ttf | |
// Run: | |
// ./hello_sdl2 | |
// Bugs: | |
// Has hard-coded path to the .ttf file. | |
// - There must be a way to say "Get the system sans serif font". | |
// - I guess I could use an environment variable to point to the location of .ttf files and other resources needed by the app. | |
// Or have well-known installation directory to look for a bundled .ttf file,like ~/.config/name-of-app or /usr/share/name-of-app | |
// Better: the $XDG_CONFIG_DIRS preference-ordered set of directories. | |
// (Does Windows have something similar?) | |
// - Update: Good blog post about general problem of how an executable can find resource files | |
// https://web.archive.org/web/20150910164707/http://voices.canonical.com/jussi.pakkanen/2011/11/15/accessing-files-how-hard-can-it-be/ | |
// Via: https://github.com/mesonbuild/meson/issues/860#issuecomment-502482754 | |
#include <stdio.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_ttf.h> | |
#define SCREEN_WIDTH 640 | |
#define SCREEN_HEIGHT 480 | |
void drawText ( SDL_Surface* screen, char* string, int size, int x, int y, SDL_Color fgC, SDL_Color bgC) { | |
// Remember to call TTF_Init(), TTF_Quit(), before/after using this function. | |
TTF_Font* font = TTF_OpenFont("/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono-Bold.ttf", size); | |
if(!font) { | |
printf("[ERROR] TTF_OpenFont() Failed with: %s\n", TTF_GetError()); | |
exit(2); | |
} | |
TTF_SetFontStyle(font, TTF_STYLE_BOLD); | |
//SDL_Surface* textSurface = TTF_RenderText_Solid(font, string, fgC); // aliased glyphs | |
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string, fgC, bgC); // anti-aliased glyphs | |
SDL_Rect textLocation = { x, y, 0, 0 }; | |
SDL_BlitSurface(textSurface, NULL, screen, &textLocation); | |
SDL_FreeSurface(textSurface); | |
TTF_CloseFont(font); | |
} | |
int main(int argc, char* args[]) { | |
SDL_Window* window = NULL; | |
SDL_Surface* screenSurface = NULL; | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError()); | |
return 1; | |
} | |
window = SDL_CreateWindow( | |
"hello_sdl2", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
SCREEN_WIDTH, SCREEN_HEIGHT, | |
SDL_WINDOW_SHOWN | |
); | |
if (window == NULL) { | |
fprintf(stderr, "could not create window: %s\n", SDL_GetError()); | |
return 1; | |
} | |
screenSurface = SDL_GetWindowSurface(window); | |
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); | |
SDL_UpdateWindowSurface(window); | |
//----------------------------------------------------- | |
// Draw some Text | |
//----------------------------------------------------- | |
if(TTF_Init() == -1) { | |
printf("[ERROR] TTF_Init() Failed with: %s\n", TTF_GetError()); | |
exit(2); | |
} | |
SDL_Color fg = { 0x00,0x00,0xff }, bg = {0xff,0xff,0xff}; // Blue text on white background | |
drawText( screenSurface, (char*) "Hello World! @ (x=50, y=100)", 18, 50, 100, fg, bg); | |
SDL_UpdateWindowSurface(window); | |
TTF_Quit(); | |
SDL_Delay(2000); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment