Advertisement
Guest User

Untitled

a guest
Feb 8th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. void gClearScreen(CColor* color)
  2. {
  3.     float r = (float)color->r / 255;
  4.     float g = (float)color->g / 255;
  5.     float b = (float)color->b / 255;
  6.  
  7.     glClearColor(r, g, b, 1.0f);
  8.     glClear(GL_COLOR_BUFFER_BIT);
  9. }
  10.  
  11. #define GL_UNSIGNED_SHORT_5_6_5 0x8363
  12. #define TEXTURE_COLORKEY 63519
  13.  
  14. void gPrepareBitmap(CBitmap* bmp)
  15. {
  16.     GLuint tex[1];
  17.     glGenTextures(1, &tex);
  18.  
  19.     sysLogf("Uploading texture %dx%d\n", bmp->width, bmp->height);
  20.  
  21.     unsigned char* data = (unsigned char*)malloc(bmp->width * bmp->height * 4);
  22.  
  23.     // Quick endian flip & color-space conversion
  24.     for (int i = 0; i < bmp->width * bmp->height; i++)
  25.     {
  26.         unsigned short pixel = *((unsigned short*)&bmp->pixels[i * 2]);
  27.  
  28.         float r = (float)(pixel & 31) / 32;
  29.         float g = (float)((pixel >> 5) & 63) / 64;
  30.         float b = (float)(pixel >> 11) / 32;
  31.  
  32.         data[i * 4 + 2] = (unsigned char)(r * 255);
  33.         data[i * 4 + 1] = (unsigned char)(g * 255);
  34.         data[i * 4] = (unsigned char)(b * 255);
  35.  
  36.         data[i * 4 + 3] = pixel == TEXTURE_COLORKEY ? 0 : 255;
  37.     }
  38.  
  39.     glBindTexture(GL_TEXTURE_2D, tex[0]);
  40.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  41.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  42.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bmp->width, bmp->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  43.  
  44.     free(data);
  45.  
  46.     bmp->_platData = tex[0];
  47. }
  48.  
  49. void gDrawBitmap(CBitmap* bmp, int x, int y)
  50. {
  51.     gDrawBitmapEx(bmp, x, y, 0, 0);
  52. }
  53.  
  54. void gDrawBitmapEx(CBitmap* bmp, int x, int y, CColor* colorKey, CColor* mulColor)
  55. {
  56.     if (!bmp->_platData)
  57.         gPrepareBitmap(bmp);
  58.  
  59.     glBindTexture(GL_TEXTURE_2D, (GLuint)bmp->_platData);
  60.  
  61.     glBegin(GL_QUADS);
  62.     glTexCoord2f(0, 0);
  63.     glVertex2i(x, y);
  64.     glTexCoord2f(1, 0);
  65.     glVertex2i(x + bmp->width, y);
  66.     glTexCoord2f(1, 1);
  67.     glVertex2i(x + bmp->width, y + bmp->height);
  68.     glTexCoord2f(0, 1);
  69.     glVertex2i(x, y + bmp->height);
  70.     glEnd();
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement