Created
June 9, 2020 12:48
-
-
Save CaptainHandyman/97ad8b6d8e3a4c615d93f279b6a56318 to your computer and use it in GitHub Desktop.
C++, SDL2, OpenGL - Texture 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 <SDL2/SDL_image.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.h> | |
#include <string> | |
using namespace std; | |
SDL_Window *window; | |
SDL_Event event; | |
SDL_Surface *texture; | |
GLuint textureID=0; | |
float x=0, y=0; | |
int mode=GL_RGB; | |
void loadTexture(string path){ | |
texture=IMG_Load(path.c_str()); | |
if(texture->format->BitsPerPixel>=4) | |
mode=GL_RGBA; | |
else | |
mode=GL_RGB; | |
glGenTextures(1, &textureID); | |
glBindTexture(GL_TEXTURE_2D, textureID); | |
glTexImage2D(GL_TEXTURE_2D, 0, mode, texture->w, texture->h, 0, | |
mode, GL_UNSIGNED_BYTE, texture->pixels); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
} | |
void displayTexture(){ | |
glEnable(GL_TEXTURE_2D); | |
glEnable(GL_BLEND); | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
glTranslatef(x, y, 0); | |
glBegin(GL_POLYGON); | |
glTexCoord2f(0, 0); glVertex2i(0, 0); | |
glTexCoord2f(1, 0); glVertex2i(texture->w, 0); | |
glTexCoord2f(1, 1); glVertex2i(texture->w, texture->h); | |
glTexCoord2f(0, 1); glVertex2i(0, texture->h); | |
glEnd(); | |
glDisable(GL_BLEND); | |
glDisable(GL_TEXTURE_2D); | |
} | |
int main(){ | |
window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, | |
SDL_WINDOWPOS_CENTERED, 800, 800, SDL_WINDOW_OPENGL); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); | |
SDL_GLContext glContext=SDL_GL_CreateContext(window); | |
SDL_GL_SetSwapInterval(SDL_FALSE); | |
loadTexture("1.png"); | |
while(window!=NULL){ | |
while(SDL_PollEvent(&event)){ | |
if(event.type==SDL_QUIT) | |
window=NULL; | |
if(event.key.keysym.scancode==SDL_SCANCODE_ESCAPE) | |
window=NULL; | |
} | |
glClearColor(0, 0, 0, 0); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0, 800, 800, 0, -10, 10); | |
displayTexture(); | |
SDL_GL_SwapWindow(window); | |
} | |
} |
hello, I have a class with these functions and I can draw 2 textures, but sometimes they are not drawn properly. Can you give me an example where you have 2 textures drawn?
https://gamedev.stackexchange.com/questions/46640/how-to-draw-2d-images-using-opengl-in-sdl
thanks but i used glPushMatrix() and glPopMatrix() and my problem was solved
thanks but i used glPushMatrix() and glPopMatrix() and my problem was solved
Ok, glad you solved that : 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, I have a class with these functions and I can draw 2 textures, but sometimes they are not drawn properly. Can you give me an example where you have 2 textures drawn?