- First commit to gitea
This commit is contained in:
71
texture.cpp
Normal file
71
texture.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "texture.h"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STBI_NO_FAILURE_STRINGS
|
||||
#include "stb_image.h"
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
|
||||
namespace Texture
|
||||
{
|
||||
unsigned texture = 0;
|
||||
|
||||
unsigned Create(const int w, const int h)
|
||||
{
|
||||
unsigned texture;
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
if (w != -1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
return texture;
|
||||
}
|
||||
|
||||
const bool Load(const unsigned& texture, const char* filename)
|
||||
{
|
||||
int x,y,n;
|
||||
char name[400]; strcpy(name, filename);
|
||||
if (strrchr(filename, '.') == nullptr) strcat(name, ".png");
|
||||
unsigned char *data = stbi_load(name, &x, &y, &n, 4);
|
||||
if (data == nullptr) return false;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
stbi_image_free(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t* Load(const char* filename)
|
||||
{
|
||||
int x,y,n;
|
||||
char name[400]; strcpy(name, filename);
|
||||
if (strrchr(filename, '.') == nullptr) strcat(name, ".png");
|
||||
unsigned char *data = stbi_load(name, &x, &y, &n, 4);
|
||||
return data;
|
||||
}
|
||||
|
||||
void Update(const unsigned& texture, const int w, const int h, const uint8_t* data)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
}
|
||||
}
|
||||
|
||||
namespace Bitmap
|
||||
{
|
||||
void PutPixel(uint8_t* data, const int x, const int y, const Color& color)
|
||||
{
|
||||
memcpy(&data[x+y*128], &color, 4);
|
||||
}
|
||||
|
||||
const Color GetPixel(uint8_t* data, const int x, const int y)
|
||||
{
|
||||
Color color;
|
||||
data += (x+y*128);
|
||||
color.r = *(data++);
|
||||
color.g = *(data++);
|
||||
color.b = *(data++);
|
||||
color.a = *(data++);
|
||||
return color;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user