Skip to content

Instantly share code, notes, and snippets.

@miguelmartin75
Last active May 3, 2023 20:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmartin75/6946310 to your computer and use it in GitHub Desktop.
Save miguelmartin75/6946310 to your computer and use it in GitHub Desktop.
A simple application with SDL (1.2 and 2), to check whether it is working or not.
#include <iostream>
#include <SDL/SDL.h>
int main(int argc, char * argv[])
{
// Initialize SDL with video
SDL_Init(SDL_INIT_VIDEO);
// Create a window with SDL
if(SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_OPENGL) == 0)
{
std::cerr << "Failed to set video mode\n";
return 1;
}
SDL_Event event; // used to store any events from the OS
bool running = true; // used to determine if we're running the game
while(running)
{
// poll for events from SDL
while(SDL_PollEvent(&event))
{
// determine if the user still wants to have the window open
// (this basically checks if the user has pressed 'X')
running = event.type != SDL_QUIT;
}
// Swap OpenGL buffers
SDL_GL_SwapBuffers();
}
// Quit SDL
SDL_Quit();
return 0;
}
#include <iostream>
#include <SDL2/SDL.h>
// if we're compiling for iOS (iPhone/iPad)
#ifdef __IPHONEOS__
# include <SDL2/SDL_opengles.h> // we want to use OpenGL ES
#else
# include <SDL2/SDL_opengl.h> // otherwise we want to use OpenGL
#endif
int main(int argc, char * argv[])
{
// Initialize SDL with video
SDL_Init(SDL_INIT_VIDEO);
// Create an SDL window
SDL_Window* window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL);
// if failed to create a window
if(!window)
{
// we'll print an error message and exit
std::cerr << "Error failed to create window!\n";
return 1;
}
// Create an OpenGL context (so we can use OpenGL functions)
SDL_GLContext context = SDL_GL_CreateContext(window);
// if we failed to create a context
if(!context)
{
// we'll print out an error message and exit
std::cerr << "Error failed to create a context\n!";
return 2;
}
SDL_Event event; // used to store any events from the OS
bool running = true; // used to determine if we're running the game
glClearColor(1, 0, 0, 1);
while(running)
{
// poll for events from SDL
while(SDL_PollEvent(&event))
{
// determine if the user still wants to have the window open
// (this basically checks if the user has pressed 'X')
running = event.type != SDL_QUIT;
}
glClear(GL_COLOR_BUFFER_BIT);
// Swap OpenGL buffers
SDL_GL_SwapWindow(window);
}
// Destroy the context
SDL_GL_DeleteContext(context);
// Destroy the window
SDL_DestroyWindow(window);
// And quit SDL
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment