Created
September 28, 2015 12:33
-
-
Save bjconlan/f25107b8976ff29fc0f1 to your computer and use it in GitHub Desktop.
SDL 2 tests
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
#define SDL_MAIN_HANDLED // resolve main symbol not sdl_main | |
#include <stdio.h> | |
#include <SDL2/SDL.h> | |
int main(int argc, char **argv) { | |
if (SDL_Init(SDL_INIT_VIDEO)) { | |
printf("SDL_Init Error: %s \n", SDL_GetError()); | |
return 1; | |
} | |
atexit(SDL_Quit); | |
SDL_Window *win = SDL_CreateWindow("Hello world", 100, 100, 640, 480, SDL_WINDOW_OPENGL); | |
if (win == NULL) { | |
fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
SDL_Renderer *rdr = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); | |
if (rdr == NULL) { | |
SDL_DestroyWindow(win); | |
fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError()); | |
return 0; | |
} | |
SDL_Surface *bmp = SDL_LoadBMP("orange.64x64.bmp"); | |
if (bmp == NULL) { | |
SDL_DestroyRenderer(rdr); | |
SDL_DestroyWindow(win); | |
fprintf(stderr, "SDL_LoadBMP Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
SDL_Texture *tex = SDL_CreateTextureFromSurface(rdr, bmp); | |
SDL_FreeSurface(bmp); | |
if (tex == NULL) { | |
SDL_DestroyRenderer(rdr); | |
SDL_DestroyWindow(win); | |
fprintf(stderr, "SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
for (int i = 0; i < 3; i++) { | |
SDL_RenderClear(rdr); | |
SDL_RenderCopy(rdr, tex, NULL, NULL); | |
SDL_RenderPresent(rdr); | |
SDL_Delay(1000); | |
} | |
SDL_DestroyTexture(tex); | |
SDL_DestroyRenderer(rdr); | |
SDL_DestroyWindow(win); | |
return 0; | |
} |
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
#define SDL_MAIN_HANDLED // resolve main symbol not sdl_main | |
#include <stdio.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
void render() { | |
glClear(GL_COLOR_BUFFER_BIT); | |
glRotatef(0.4f,0.0f,1.0f,0.0f); // Rotate The cube around the Y axis | |
glRotatef(0.2f,1.0f,1.0f,1.0f); | |
glColor3f(0.0f,1.0f,0.0f); | |
glBegin(GL_QUADS); | |
glVertex2f(-0.5f, -0.5f); | |
glVertex2f(0.5f, -0.5f); | |
glVertex2f(0.5f, 0.5f); | |
glVertex2f(-0.5f, 0.5f); | |
glEnd(); | |
} | |
void initOpenGL() { | |
GLenum error = GL_NO_ERROR; | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "GL matrix mode (projection) Error: %d", error); | |
return 1; | |
} | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
error = glGetError(); | |
if (error != GL_NO_ERROR) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "GL matrix mode (model view) Error: %d", error); | |
return 1; | |
} | |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // check error here too? | |
} | |
int main(int argc, char **argv) { | |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); | |
if (SDL_Init(SDL_INIT_VIDEO)) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init Error: %s \n", SDL_GetError()); | |
return 1; | |
} | |
atexit(SDL_Quit); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); | |
SDL_Window *win = SDL_CreateWindow("Hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL); | |
if (win == NULL) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
SDL_GLContext *ctx = SDL_GL_CreateContext(win); | |
if (ctx == NULL) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindow Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
if (SDL_GL_SetSwapInterval(1) < 0) { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unabled to set VSync Error: %s\n", SDL_GetError()); | |
} | |
initOpenGL(); | |
// setup event loop | |
for (;;) { | |
SDL_Event e; | |
if (SDL_PollEvent(&e)) { | |
if (e.type == SDL_QUIT) | |
break; | |
if (e.key.keysym.sym == SDLK_ESCAPE) | |
break; | |
} | |
render(); | |
SDL_GL_SwapWindow(win); | |
} | |
SDL_DestroyWindow(win); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment