Use OpenGLES on Linux by default.

* Add error checking on shader compilation.
* Use 2 versions of fragmentShader for GLSL 1.0 and GLSL 2.0 or later.
This commit is contained in:
2023-09-24 20:58:23 +02:00
parent 865ce5bcc5
commit e6e1845743
2 changed files with 92 additions and 111 deletions

View File

@@ -6,13 +6,14 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/" "${CMAKE
if (EMSCRIPTEN) if (EMSCRIPTEN)
message(" * Ignore SDL2 package with Emscripten") message(" * Ignore SDL2 package with Emscripten")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_SDL=2") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_SDL=2 -Wno-implicit-function-declaration -DEMSCRIPTEN")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_SDL=2 -s TOTAL_MEMORY=134217728 -lidbfs.js")
else() else()
find_package(SDL2 REQUIRED) find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS}) include_directories(${SDL2_INCLUDE_DIRS})
endif() endif()
set(OpenGL_GL_PREFERENCE LEGACY) set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)

View File

@@ -1,6 +1,7 @@
// Copyright (C) 2011-2015 Valeriano Alfonso Rodriguez (Kableado) // Copyright (C) 2011-2015 Valeriano Alfonso Rodriguez (Kableado)
#ifdef WIN32 #ifdef WIN32
// Windows // Windows
#define _WIN32_WINNT 0x0501 #define _WIN32_WINNT 0x0501
#include <GL/gl.h> #include <GL/gl.h>
@@ -13,33 +14,24 @@
#define USE_OpenGL 1 #define USE_OpenGL 1
#define USE_OpenGLES 0 #define USE_OpenGLES 0
#include <unistd.h> #include <unistd.h>
#else #else
#ifdef EMSCRIPTEN
// Emscripten // Linux
#include <GLES2/gl2.h>
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define GL_GLEXT_PROTOTYPES 1 #define GL_GLEXT_PROTOTYPES 1
#include <GLES2/gl2ext.h> #include <SDL_opengl.h>
#include <emscripten.h>
#define USE_OpenGL 0 #define USE_OpenGL 0
#define USE_OpenGLES 1 #define USE_OpenGLES 1
#define SDL_GetKeyState SDL_GetKeyboardState // #define SDL_GetKeyState SDL_GetKeyboardState
#include <unistd.h> #ifdef EMSCRIPTEN
#else #include <emscripten.h>
// UNIX
#include <GL/gl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USE_OpenGL 1
#define USE_OpenGLES 0
#include <unistd.h>
#endif #endif
#endif #endif
#include "lodepng.c" #include "lodepng.c"
#include <SDL.h> #include <SDL.h>
@@ -70,6 +62,7 @@ struct TDrawImage {
// Globals // Globals
static SDL_Window *_window = NULL; static SDL_Window *_window = NULL;
static SDL_GLContext _glcontext = NULL; static SDL_GLContext _glcontext = NULL;
static SDL_Renderer *_renderer = NULL;
int _width; int _width;
int _height; int _height;
long long proc_t_frame = 33333; long long proc_t_frame = 33333;
@@ -84,8 +77,12 @@ float _color[4];
GLuint _whiteTex; GLuint _whiteTex;
GLuint Draw_CompileShader(GLenum type, const char *source) { GLuint Draw_CompileShader(GLenum type, const char *source) {
char *strType = type == GL_VERTEX_SHADER ? "VertexShader"
: type == GL_FRAGMENT_SHADER ? "fragmentShader"
: "unknownShader";
GLuint shader = glCreateShader(type); GLuint shader = glCreateShader(type);
if (shader == 0) { if (shader == 0) {
Print("Error creating shader of type: %s\n", strType);
return 0; return 0;
} }
@@ -97,6 +94,12 @@ GLuint Draw_CompileShader(GLenum type, const char *source) {
GLint compiled; GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) { if (!compiled) {
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar strInfoLog[infoLogLength + 1];
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
Print("Error compiling shader of type: %s: %s\n", strType, strInfoLog);
glDeleteShader(shader); glDeleteShader(shader);
return 0; return 0;
} }
@@ -104,13 +107,10 @@ GLuint Draw_CompileShader(GLenum type, const char *source) {
return shader; return shader;
} }
GLuint Draw_BuildProgram(const char *vertexShaderSource, GLuint Draw_BuildProgram(const char *vertexShaderSource, const char *fragmentShaderSource) {
const char *fragmentShaderSource) {
// Compile shaders // Compile shaders
GLuint vertexShader = GLuint vertexShader = Draw_CompileShader(GL_VERTEX_SHADER, vertexShaderSource);
Draw_CompileShader(GL_VERTEX_SHADER, vertexShaderSource); GLuint fragmentShader = Draw_CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
GLuint fragmentShader =
Draw_CompileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (vertexShader == 0 || fragmentShader == 0) { if (vertexShader == 0 || fragmentShader == 0) {
return 0; return 0;
} }
@@ -187,15 +187,17 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
} }
// Initialize video mode // Initialize video mode
_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, _window =
SDL_WINDOWPOS_UNDEFINED, width, height, SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
SDL_WINDOW_OPENGL);
if (_window == NULL) { if (_window == NULL) {
Print("Draw_Init: Failure initializing video mode.\n"); Print("Draw_Init: Failure initializing video mode.\n");
Print("\tSDL Error: %s\n", SDL_GetError()); Print("\tSDL Error: %s\n", SDL_GetError());
return (0); return (0);
} }
_glcontext = SDL_GL_CreateContext(_window); _glcontext = SDL_GL_CreateContext(_window);
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
Draw_ShowInfo(); Draw_ShowInfo();
#if USE_OpenGL #if USE_OpenGL
@@ -237,8 +239,7 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
" vColor = aColor; \n" " vColor = aColor; \n"
"} \n"; "} \n";
const char fragmentShaderSource[] = const char fragmentShaderSource_10[] = "precision mediump float; \n"
"precision mediump float; \n"
"varying vec2 vTexCoord; \n" "varying vec2 vTexCoord; \n"
"varying vec4 vColor; \n" "varying vec4 vColor; \n"
"uniform sampler2D sTexture; \n" "uniform sampler2D sTexture; \n"
@@ -246,7 +247,17 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
" gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n" " gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n"
"} \n"; "} \n";
programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource); const char fragmentShaderSource_20[] = "varying vec2 vTexCoord; \n"
"varying vec4 vColor; \n"
"uniform sampler2D sTexture; \n"
"void main() { \n"
" gl_FragColor = texture2D(sTexture, vTexCoord)*vColor; \n"
"} \n";
programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_20);
if (programObject == 0) {
programObject = Draw_BuildProgram(vertexShaderSource, fragmentShaderSource_10);
}
glUseProgram(programObject); glUseProgram(programObject);
vertPosLoc = glGetAttribLocation(programObject, "aPosition"); vertPosLoc = glGetAttribLocation(programObject, "aPosition");
@@ -254,27 +265,21 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
vertColorLoc = glGetAttribLocation(programObject, "aColor"); vertColorLoc = glGetAttribLocation(programObject, "aColor");
textureLoc = glGetUniformLocation(programObject, "sTexture"); textureLoc = glGetUniformLocation(programObject, "sTexture");
projectionMatrixLoc = projectionMatrixLoc = glGetUniformLocation(programObject, "sProjectionMatrix");
glGetUniformLocation(programObject, "sProjectionMatrix");
glUniform1i(textureLoc, 0); glUniform1i(textureLoc, 0);
glGenBuffers(1, &vertexObject); glGenBuffers(1, &vertexObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexObject); glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
glBufferData(GL_ARRAY_BUFFER, glBufferData(GL_ARRAY_BUFFER, Vertex2D_Length * sizeof(float) * Max_Vertices, NULL, GL_DYNAMIC_DRAW);
Vertex2D_Length * sizeof(float) * Max_Vertices, NULL,
GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexObject); glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
glVertexAttribPointer(vertPosLoc, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertPosLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float),
Vertex2D_Length * sizeof(float),
(void *)(0 * sizeof(float))); (void *)(0 * sizeof(float)));
glVertexAttribPointer(vertTexLoc, 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertTexLoc, 2, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float),
Vertex2D_Length * sizeof(float),
(void *)(2 * sizeof(float))); (void *)(2 * sizeof(float)));
glVertexAttribPointer(vertColorLoc, 4, GL_FLOAT, GL_FALSE, glVertexAttribPointer(vertColorLoc, 4, GL_FLOAT, GL_FALSE, Vertex2D_Length * sizeof(float),
Vertex2D_Length * sizeof(float),
(void *)(4 * sizeof(float))); (void *)(4 * sizeof(float)));
glEnableVertexAttribArray(vertPosLoc); glEnableVertexAttribArray(vertPosLoc);
@@ -288,8 +293,7 @@ int Draw_Init(int width, int height, char *title, int pfps, int fps) {
// Set the proyection (2D) // Set the proyection (2D)
glViewport(0, 0, _width, _height); glViewport(0, 0, _width, _height);
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0, float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
0, 0, 1, 0, 0, 0, 0, 1};
projectionMatrix[0] = (2.0f / _width); projectionMatrix[0] = (2.0f / _width);
projectionMatrix[5] = -(2.0f / _height); projectionMatrix[5] = -(2.0f / _height);
projectionMatrix[10] = -0.001f; projectionMatrix[10] = -0.001f;
@@ -323,6 +327,12 @@ void Draw_ShowInfo() {
Print(" Renderer: %s\n", str); Print(" Renderer: %s\n", str);
str = (char *)glGetString(GL_VERSION); str = (char *)glGetString(GL_VERSION);
Print(" Version: %s\n", str); Print(" Version: %s\n", str);
GLint major, minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
Print(" Version : %d.%d\n", major, minor);
str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
Print(" Shader Version: %s\n", str);
Print("*********************************\n"); Print("*********************************\n");
} }
@@ -332,10 +342,8 @@ void Draw_ShowInfo() {
// Sets the render matrix // Sets the render matrix
void Draw_SetMatrix(float matrix[16]) { void Draw_SetMatrix(float matrix[16]) {
#if USE_OpenGL #if USE_OpenGL
float tempMatrix[16] = {matrix[0], matrix[4], matrix[8], matrix[12], float tempMatrix[16] = {matrix[0], matrix[4], matrix[8], matrix[12], matrix[1], matrix[5], matrix[9], matrix[13],
matrix[1], matrix[5], matrix[9], matrix[13], matrix[2], matrix[6], matrix[10], matrix[14], matrix[3], matrix[7], matrix[11], matrix[15]};
matrix[2], matrix[6], matrix[10], matrix[14],
matrix[3], matrix[7], matrix[11], matrix[15]};
glLoadMatrixf(tempMatrix); glLoadMatrixf(tempMatrix);
#endif #endif
#if USE_OpenGLES #if USE_OpenGLES
@@ -364,8 +372,7 @@ GLuint Draw_UploadGLTexture(int w, int h, unsigned char *pixels) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, w); glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
#endif #endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
pixels);
return (tex); return (tex);
} }
@@ -379,28 +386,22 @@ void Draw_Flush() {
return; return;
} }
if (_currentImg->tex == -1) { if (_currentImg->tex == -1) {
_currentImg->tex = Draw_UploadGLTexture(_currentImg->w, _currentImg->h, _currentImg->tex = Draw_UploadGLTexture(_currentImg->w, _currentImg->h, _currentImg->data);
_currentImg->data);
} }
#if USE_OpenGL #if USE_OpenGL
// Draw the quad array // Draw the quad array
glBindTexture(GL_TEXTURE_2D, _currentImg->tex); glBindTexture(GL_TEXTURE_2D, _currentImg->tex);
glColorPointer(4, GL_FLOAT, Vertex2D_Length * sizeof(float), glColorPointer(4, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData + 4));
(GLvoid *)(_quadArray->vertexData + 4)); glTexCoordPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData + 2));
glTexCoordPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), glVertexPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float), (GLvoid *)(_quadArray->vertexData));
(GLvoid *)(_quadArray->vertexData + 2));
glVertexPointer(2, GL_FLOAT, Vertex2D_Length * sizeof(float),
(GLvoid *)(_quadArray->vertexData));
glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex); glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex);
#else #else
// Draw the quad array // Draw the quad array
glBindTexture(GL_TEXTURE_2D, _currentImg->tex); glBindTexture(GL_TEXTURE_2D, _currentImg->tex);
glBufferSubData(GL_ARRAY_BUFFER, 0, glBufferSubData(GL_ARRAY_BUFFER, 0, Vertex2D_Length * sizeof(float) * _quadArray->nVertex, _quadArray->vertexData);
Vertex2D_Length * sizeof(float) * _quadArray->nVertex,
_quadArray->vertexData);
glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex); glDrawArrays(GL_TRIANGLES, 0, _quadArray->nVertex);
#endif #endif
@@ -414,7 +415,7 @@ void Draw_Flush() {
// //
// Cleans the game window. // Cleans the game window.
void Draw_Clean(unsigned char r, unsigned char g, unsigned char b) { void Draw_Clean(unsigned char r, unsigned char g, unsigned char b) {
#ifndef EMSCRIPTEN #if USE_OpenGL
glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f); glClearColor(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
#else #else
@@ -478,12 +479,10 @@ int Draw_LoopIteration() {
} }
} }
if (event.type == SDL_MOUSEMOTION) { if (event.type == SDL_MOUSEMOTION) {
Input_SetPointerPosition(event.motion.x / (float)_width, Input_SetPointerPosition(event.motion.x / (float)_width, event.motion.y / (float)_height);
event.motion.y / (float)_height);
} }
if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.type == SDL_MOUSEBUTTONDOWN) {
Input_SetPointerPosition(event.button.x / (float)_width, Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
event.button.y / (float)_height);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_FINGERMOTION) { if (event.type == SDL_FINGERMOTION) {
@@ -494,8 +493,7 @@ int Draw_LoopIteration() {
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_MOUSEBUTTONUP) { if (event.type == SDL_MOUSEBUTTONUP) {
Input_SetPointerPosition(event.button.x / (float)_width, Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
event.button.y / (float)_height);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
if (event.type == SDL_FINGERUP) { if (event.type == SDL_FINGERUP) {
@@ -520,17 +518,14 @@ int Draw_LoopIteration() {
} }
} }
if (event.type == SDL_MOUSEMOTION) { if (event.type == SDL_MOUSEMOTION) {
Input_SetPointerPosition(event.motion.x / (float)_width, Input_SetPointerPosition(event.motion.x / (float)_width, event.motion.y / (float)_height);
event.motion.y / (float)_height);
} }
if (event.type == SDL_MOUSEBUTTONDOWN) { if (event.type == SDL_MOUSEBUTTONDOWN) {
Input_SetPointerPosition(event.button.x / (float)_width, Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
event.button.y / (float)_height);
Input_SetPointerDown(1); Input_SetPointerDown(1);
} }
if (event.type == SDL_MOUSEBUTTONUP) { if (event.type == SDL_MOUSEBUTTONUP) {
Input_SetPointerPosition(event.button.x / (float)_width, Input_SetPointerPosition(event.button.x / (float)_width, event.button.y / (float)_height);
event.button.y / (float)_height);
Input_SetPointerDown(0); Input_SetPointerDown(0);
} }
} }
@@ -579,8 +574,7 @@ void Draw_LoopIterationAux() {
// Draw_Loop // Draw_Loop
// //
// Loops updating the game window. // Loops updating the game window.
void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f), void Draw_Loop(void (*proc)(void *data), void (*draw)(void *data, float f), void *data) {
void *data) {
_proc_func = proc; _proc_func = proc;
_draw_func = draw; _draw_func = draw;
@@ -662,12 +656,9 @@ DrawImg Draw_LoadImage(char *filename) {
// Try loading PNG images // Try loading PNG images
if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) { if (EndsWith(filename, ".png") || EndsWith(filename, ".PNG")) {
image = malloc(sizeof(TDrawImage)); image = malloc(sizeof(TDrawImage));
unsigned error = unsigned error = lodepng_decode32_file(&image->data, (unsigned *)&image->w, (unsigned *)&image->h, filename);
lodepng_decode32_file(&image->data, (unsigned *)&image->w,
(unsigned *)&image->h, filename);
if (error) { if (error) {
Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n", Print("Draw_LoadImage: PNG decoder error %u: %s on file %s\n", error, lodepng_error_text(error), filename);
error, lodepng_error_text(error), filename);
return (NULL); return (NULL);
} }
image->x = -(int)(image->w / 2); image->x = -(int)(image->w / 2);
@@ -803,8 +794,7 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h) {
// Draw_DrawImgPart // Draw_DrawImgPart
// //
// Draws an image part. // Draws an image part.
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, float scale[2]) {
float scale[2]) {
DrawImage image = img; DrawImage image = img;
int x1, x2, y1, y2; int x1, x2, y1, y2;
float us, u1, u2; float us, u1, u2;
@@ -848,8 +838,7 @@ void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j,
// Draw_DrawImgPartHoriz // Draw_DrawImgPartHoriz
// //
// Draws an image part horizontally. // Draws an image part horizontally.
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[2]) {
float scale[2]) {
DrawImage image = img; DrawImage image = img;
int x1, x2, y1, y2; int x1, x2, y1, y2;
float us, u1, u2; float us, u1, u2;
@@ -890,8 +879,7 @@ void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i,
// Draw_ImgParallax // Draw_ImgParallax
// //
// //
void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], void Draw_ImgParallax(DrawImg img, int imgSize[2], int imgOffset[2], float parallaxFactor[2], int gamePos[2],
float parallaxFactor[2], int gamePos[2],
int gameSize[2]) { int gameSize[2]) {
int paralaxPos[2]; int paralaxPos[2];
int mult[2]; int mult[2];
@@ -947,8 +935,7 @@ typedef struct {
// //
// Creates a image with the default font. // Creates a image with the default font.
#include "FontData.h" #include "FontData.h"
DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g, DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
unsigned char b, unsigned char a) {
DrawImage img; DrawImage img;
int x, y, c; int x, y, c;
@@ -979,8 +966,7 @@ DrawImage Draw_DefaultFontImage(unsigned char r, unsigned char g,
// Draw_DefaultFont // Draw_DefaultFont
// //
// Creates the default font. // Creates the default font.
DrawFnt Draw_DefaultFont(unsigned char r, unsigned char g, unsigned char b, DrawFnt Draw_DefaultFont(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
unsigned char a) {
DrawFont *font; DrawFont *font;
// Create the default font // Create the default font
@@ -1038,8 +1024,7 @@ void Draw_DrawText(DrawFnt f, char *text, int x, int y) {
ptr = text; ptr = text;
while (*ptr) { while (*ptr) {
if ((*ptr) < font->max) { if ((*ptr) < font->max) {
Draw_DrawImgPartHoriz(font->img, x, y, font->w, (*ptr) - font->min, Draw_DrawImgPartHoriz(font->img, x, y, font->w, (*ptr) - font->min, font->scale);
font->scale);
} }
x += font->w * font->scale[0]; x += font->w * font->scale[0];
ptr++; ptr++;
@@ -1050,15 +1035,14 @@ void Draw_DrawText(DrawFnt f, char *text, int x, int y) {
// Draw_SaveRGBAToBMP // Draw_SaveRGBAToBMP
// //
// //
void Draw_SaveRGBAToBMP(char *filename, unsigned char *data, int width, void Draw_SaveRGBAToBMP(char *filename, unsigned char *data, int width, int height) {
int height) {
SDL_Surface *surf; SDL_Surface *surf;
// Create the surface // Create the surface
surf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); surf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0);
surf->format->Amask = 0xFF000000; surf->format->Amask = 0xFF000000;
surf->format->Ashift = 24; surf->format->Ashift = 24;
//SDL_SetAlpha(surf, GL_SRC_ALPHA, 255); // SDL_SetAlpha(surf, GL_SRC_ALPHA, 255);
SDL_LockSurface(surf); SDL_LockSurface(surf);
memcpy(surf->pixels, data, width * height * 4); memcpy(surf->pixels, data, width * height * 4);
SDL_UnlockSurface(surf); SDL_UnlockSurface(surf);
@@ -1088,12 +1072,10 @@ void Draw_SaveRGBAToBMP(char *filename, unsigned char *data, int width,
// Draw_SaveRGBAToPNG // Draw_SaveRGBAToPNG
// //
// //
void Draw_SaveRGBAToPNG(char *filename, unsigned char *data, int width, void Draw_SaveRGBAToPNG(char *filename, unsigned char *data, int width, int height) {
int height) {
unsigned error = lodepng_encode32_file(filename, data, width, height); unsigned error = lodepng_encode32_file(filename, data, width, height);
if (error) { if (error) {
Print("Draw_SaveRGBAToPNG: Error %u: %s\n", error, Print("Draw_SaveRGBAToPNG: Error %u: %s\n", error, lodepng_error_text(error));
lodepng_error_text(error));
} }
} }
@@ -1117,10 +1099,8 @@ void Draw_SaveScreenshoot(char *filename) {
image_line = malloc(line_size); image_line = malloc(line_size);
for (i = 0; i < half_height; i++) { for (i = 0; i < half_height; i++) {
memcpy(image_line, pixelData + i * line_size, line_size); memcpy(image_line, pixelData + i * line_size, line_size);
memcpy(pixelData + i * line_size, memcpy(pixelData + i * line_size, pixelData + (_height - (i + 1)) * line_size, line_size);
pixelData + (_height - (i + 1)) * line_size, line_size); memcpy(pixelData + (_height - (i + 1)) * line_size, image_line, line_size);
memcpy(pixelData + (_height - (i + 1)) * line_size, image_line,
line_size);
} }
// Save the image // Save the image