Created
September 5, 2013 13:34
-
-
Save anonymous/6450139 to your computer and use it in GitHub Desktop.
SDL2 OpenGL + Normal renderer example
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
#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