Code:
#include "graphics.h"
SDL_Window* graphics_window = NULL;
SDL_GLContext graphics_context;
// Textures
typedef struct graphics_texture {
char* image;
GLuint id;
SDL_Surface* surface;
int mode;
}graphics_texture;
graphics_texture noise = {"../media/noise.bmp", 0};
// Texture functions
int graphics_texturecreate(graphics_texture* texture) {
texture->surface = SDL_LoadBMP(texture->image);
if(texture->surface == NULL) {return 1;}
glGenTextures(1, &texture->id);
glBindTexture(GL_TEXTURE_2D, texture->id);
texture->mode = GL_RGB;
glTexImage2D(GL_TEXTURE_2D, 0, texture->mode, texture->surface->w, texture->surface->h, 0, texture->mode, GL_UNSIGNED_BYTE, texture->surface->pixels);
SDL_FreeSurface(texture->surface);
return 0;
}
int graphics_init(void) {
// Initialize SDL2
if(SDL_Init(SDL_INIT_VIDEO)) {
printf("Could not initialize SDL2: %s\n", SDL_GetError());
return 1;
}
// Create SDL2 window
graphics_window = SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, GRAPHICS_WIDTH, GRAPHICS_HEIGHT, SDL_WINDOW_OPENGL);
if(graphics_window == NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// Initialize GL context
graphics_context = SDL_GL_CreateContext(graphics_window);
// Initialize OpenGL
glClearColor(0.0, 0.0, 0.0, 1.0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
glViewport(0, 0, GRAPHICS_WIDTH, GRAPHICS_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, GRAPHICS_WIDTH, GRAPHICS_HEIGHT, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
// Textures
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(graphics_texturecreate(&noise)) {
printf("Could not create texture: %s\n", SDL_GetError());
return 1;
}
return 0;
}
void graphics_render(void) {
// Rendering
glClear(GL_COLOR_BUFFER_BIT);
// Draw a textured quad
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_QUADS);
//glColor3ub(255, 0, 0);
glTexCoord2f(0, 0);
glVertex2i(100, 100);
//glColor3ub(0, 255, 0);
glTexCoord2f(0, 1);
glVertex2i(100, 164);
//glColor3ub(0, 0, 255);
glTexCoord2f(1, 1);
glVertex2i(164, 164);
//glColor3ub(255, 0, 255);
glTexCoord2f(1, 0);
glVertex2i(164, 100);
glEnd();
// Present frame
SDL_GL_SwapWindow(graphics_window);
}
void graphics_close(void) {
// Clean up
SDL_DestroyWindow(graphics_window);
SDL_GL_DeleteContext(graphics_context);
// Exit SDL2
SDL_Quit();
}
keep in mind that I am still looking at what the best structure for my