Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2013 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6450139 to your computer and use it in GitHub Desktop.
Save anonymous/6450139 to your computer and use it in GitHub Desktop.
SDL2 OpenGL + Normal renderer example
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
void init_opengl()
{
glShadeModel( GL_SMOOTH );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
void init_viewport(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -10, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void render(SDL_Renderer *renderer, SDL_Texture *tex)
{
SDL_RenderClear(renderer);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glColor4f( 1.0f, 0.0f, 0.0f, 0.5f );
glBegin( GL_TRIANGLES );
glVertex3f( 0.0f, 100.0f, 1.0f );
glVertex3f( 200.0f, 200.0f, 1.0f );
glVertex3f( 100.0f, 200.0f, 1.0f );
glEnd( );
SDL_Rect dst;
dst.x = 200;
dst.y = 200;
dst.w = 100;
dst.h = 100;
SDL_RenderCopyEx(renderer, tex, NULL, &dst, 0, NULL, 0);
SDL_RenderPresent(renderer);
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window;
SDL_Renderer* renderer;
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &window, &renderer);
SDL_Surface *surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0);
Uint32 blue = SDL_MapRGBA(surface->format, 0, 255, 0, 128);
SDL_FillRect(surface, NULL, blue);
SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
init_opengl();
init_viewport(800, 600);
SDL_Event event;
bool running = true;
while(running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
break;
}
}
render(renderer, tex);
SDL_Delay(1000);
}
SDL_DestroyTexture(tex);
SDL_Quit();
(void) argc;
(void) argv;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment