Add abstraction for GPU shaders

This commit is contained in:
Sebastian Krzyszkowiak 2018-05-30 21:11:46 +02:00
parent 52883755f1
commit 9182cabf79
8 changed files with 104 additions and 3 deletions

View file

@ -7,6 +7,7 @@ SET(SRC_LIST
character.c
internal.c
tween.c
shader.c
)
if (EMSCRIPTEN)
list(APPEND SRC_LIST emscripten-audio-stream.c)

View file

@ -351,9 +351,16 @@ SYMBOL_INTERNAL struct libsuperderpy_list* AddToList(struct libsuperderpy_list*
return list;
}
SYMBOL_INTERNAL struct libsuperderpy_list* RemoveFromList(struct libsuperderpy_list** list, bool (*identity)(struct libsuperderpy_list* elem, void* data), void* data) {
static bool Identity(struct libsuperderpy_list* elem, void* data) {
return elem->data == data;
}
SYMBOL_INTERNAL struct libsuperderpy_list* RemoveFromList(struct libsuperderpy_list** list, void* data, bool (*identity)(struct libsuperderpy_list* elem, void* data)) {
struct libsuperderpy_list *prev = NULL, *tmp = *list, *start;
void* d = NULL;
if (!identity) {
identity = Identity;
}
while (tmp) {
if (identity(tmp, data)) {
if (prev) {

View file

@ -80,7 +80,7 @@ void* AddGarbage(struct Game* game, void* data);
void ClearGarbage(struct Game* game);
void ClearScreen(struct Game* game);
struct libsuperderpy_list* AddToList(struct libsuperderpy_list* list, void* data);
struct libsuperderpy_list* RemoveFromList(struct libsuperderpy_list** list, bool (*identity)(struct libsuperderpy_list* elem, void* data), void* data);
struct libsuperderpy_list* RemoveFromList(struct libsuperderpy_list** list, void* data, bool (*identity)(struct libsuperderpy_list* elem, void* data));
void AddTimeline(struct Game* game, struct Timeline* timeline);
void RemoveTimeline(struct Game* game, struct Timeline* timeline);
void DrawTimelines(struct Game* game);

View file

@ -73,6 +73,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
game->_priv.garbage = NULL;
game->_priv.timelines = NULL;
game->_priv.shaders = NULL;
game->_priv.paused = false;

View file

@ -43,6 +43,7 @@ struct Game;
#include "character.h"
#include "config.h"
#include "gamestate.h"
#include "shader.h"
#include "timeline.h"
#include "tween.h"
#include "utils.h"
@ -123,7 +124,7 @@ struct Game {
struct Gamestate* current_gamestate;
struct libsuperderpy_list *garbage, *timelines;
struct libsuperderpy_list *garbage, *timelines, *shaders;
bool draw;

60
src/shader.c Normal file
View file

@ -0,0 +1,60 @@
/*! \file shader.c
* \brief GPU shaders abstraction.
*/
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "shader.h"
#include "internal.h"
#include <allegro5/allegro.h>
#include <math.h>
ALLEGRO_SHADER* CreateShader(struct Game* game, const char* vertex, const char* fragment) {
const char* log;
PrintConsole(game, "Creating shader V:%s F:%s...", vertex, fragment);
ALLEGRO_SHADER* shader = al_create_shader(ALLEGRO_SHADER_GLSL);
if (!al_attach_shader_source_file(shader, ALLEGRO_VERTEX_SHADER, vertex)) {
log = al_get_shader_log(shader);
if (log) {
PrintConsole(game, "%s", log);
}
}
if (!al_attach_shader_source_file(shader, ALLEGRO_PIXEL_SHADER, fragment)) {
log = al_get_shader_log(shader);
if (log) {
PrintConsole(game, "%s", log);
}
}
if (!al_build_shader(shader)) {
log = al_get_shader_log(shader);
if (log) {
PrintConsole(game, "%s", log);
}
}
game->_priv.shaders = AddToList(game->_priv.shaders, shader);
PrintConsole(game, "Shader compiled successfully.");
return shader;
}
void DestroyShader(struct Game* game, ALLEGRO_SHADER* shader) {
RemoveFromList(&game->_priv.shaders, shader, NULL);
al_destroy_shader(shader);
}

29
src/shader.h Normal file
View file

@ -0,0 +1,29 @@
/*! \file shader.h
* \brief GPU shaders abstraction.
*/
/*
* Copyright (c) Sebastian Krzyszkowiak <dos@dosowisko.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBSUPERDERPY_SHADER_H
#define LIBSUPERDERPY_SHADER_H
#include "libsuperderpy.h"
ALLEGRO_SHADER *CreateShader(struct Game *game, const char* vertex, const char* fragment);
void DestroyShader(struct Game *game, ALLEGRO_SHADER *shader);
#endif

View file

@ -27,6 +27,8 @@
#include <stdio.h>
#include <string.h>
// TODO: split to separate files
SYMBOL_EXPORT void DrawVerticalGradientRect(float x, float y, float w, float h, ALLEGRO_COLOR top, ALLEGRO_COLOR bottom) {
ALLEGRO_VERTEX v[] = {
{.x = x, .y = y, .z = 0, .color = top},