MCPcopy Create free account
hub / github.com/InkboxSoftware/all-screen-keyboard / SDLsurfaceToGLuint

Function SDLsurfaceToGLuint

keyboard/src/texture.cpp:50–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50GLuint SDLsurfaceToGLuint(SDL_Surface* textureSurface, const char* filename){
51 //determine file format
52 //ONLY SUPPORTS 24-BIT AND 32-BIT INTEGER IMAGES
53 GLenum format;
54 GLenum type = GL_UNSIGNED_BYTE;
55 switch (textureSurface->format->BytesPerPixel){
56 case 3:
57 format = GL_RGB;
58 break;
59 case 4:
60 format = GL_RGBA;
61 break;
62 default:
63 SDL_Log("Image is not a 24 or 32-bit color image: %s\n", filename);
64 SDL_FreeSurface(textureSurface);
65 textureSurface = NULL;
66 return 0;
67 }
68
69 //create the texture
70 GLuint texture;
71 glGenTextures(1, &texture);
72 //SDL_Log("vt: %d", texture);
73 glBindTexture(GL_TEXTURE_2D, texture);
74 glTexImage2D(GL_TEXTURE_2D, 0, format, textureSurface->w, textureSurface->h, 0, format, type, textureSurface->pixels);
75 GLenum err = glGetError();
76 if (err != GL_NO_ERROR){
77 SDL_Log("Creating texture from %s failed with error code %u\n", filename, err);
78 glDeleteBuffers(1, &texture);
79 texture = 0;
80 SDL_FreeSurface(textureSurface);
81 textureSurface = NULL;
82 return 0;
83 }
84
85 //set up texture swizzling to match image channel order
86 bool OK = SDLToGLSwizzle(GL_TEXTURE_SWIZZLE_R, textureSurface->format->Rmask);
87 OK &= SDLToGLSwizzle(GL_TEXTURE_SWIZZLE_G, textureSurface->format->Gmask);
88 OK &= SDLToGLSwizzle(GL_TEXTURE_SWIZZLE_B, textureSurface->format->Bmask);
89 if (format == GL_RGBA){
90 OK &= SDLToGLSwizzle(GL_TEXTURE_SWIZZLE_A, textureSurface->format->Amask);
91 }
92 if (!OK){
93 SDL_Log("Could not swizzle texture %s\n", filename);
94 glDeleteBuffers(1, &texture);
95 texture = 0;
96 SDL_FreeSurface(textureSurface);
97 textureSurface = NULL;
98 return 0;
99 }
100
101 //set up filtering
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
105
106 //free memory
107 SDL_FreeSurface(textureSurface); //surface is now in vram, no need to keep in ram

Callers 3

textureLoadFunction · 0.85
textureLoadFromFontFunction · 0.85
textureFromSDLSurfaceFunction · 0.85

Calls 1

SDLToGLSwizzleFunction · 0.85

Tested by

no test coverage detected