summaryrefslogtreecommitdiffstats
path: root/common.c
blob: e9258c1d4c6f130809abb90223b8a2cf940e37b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
  Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "common.h"

#include <stdio.h>

#include <GL/glew.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>


char *filename;
float current_time = 0;

GLint param0 = 0;
GLint param1 = 0;
GLint param2 = 0;


static GLint time_loc;
static GLint res_loc;

static GLint param0_loc;
static GLint param1_loc;
static GLint param2_loc;


static char * readfile(const char *name) {
	FILE *f = fopen(name, "r");
	if (!f)
		return NULL;

	size_t size = 1024;
	char *buffer = malloc(size+1);
	size_t count = 0, r;

	do {
		if (count == size) {
			size *= 2;
			buffer = realloc(buffer, size+1);
		}

		r = fread(buffer+count, 1, size-count, f);
		count += r;
	} while (r);

	fclose(f);

	buffer[count] = 0;

	return buffer;
}

static void printShaderInfoLog(GLuint obj) {
	GLint length = 0;

	glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &length);

	if(length > 0) {
		char log[length];

		glGetShaderInfoLog(obj, sizeof(log), NULL, log);
		fprintf(stderr, "%s\n", log);
	}
}

static void printProgramInfoLog(GLuint obj) {
	GLint length = 0;

	glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &length);

	if(length > 0) {
		char log[length];

		glGetProgramInfoLog(obj, sizeof(log), NULL, log);
		fprintf(stderr, "%s\n", log);
	}
}

void load(void) {
	char *shader = readfile(filename);
	if (!shader) {
		fprintf(stderr, "Error: unable to read '%s'\n", filename);
		return;
	}

	GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(frag, 1, (const GLchar **)&shader, NULL);
	glCompileShader(frag);
	printShaderInfoLog(frag);

	GLuint program = glCreateProgram();
	glAttachShader(program, frag);

	glLinkProgram(program);
	printProgramInfoLog(program);

	glBindFragDataLocation(program, 0, "fragColor");
	time_loc = glGetUniformLocation(program, "time");
	res_loc = glGetUniformLocation(program, "res");

	param0_loc = glGetUniformLocation(program, "param0");
	param1_loc = glGetUniformLocation(program, "param1");
	param2_loc = glGetUniformLocation(program, "param2");

	glUseProgram(program);

	free(shader);
	glDeleteShader(frag);
	glDeleteProgram(program);
}

void init(void) {
	glewInit();
	glDisable(GL_DEPTH_TEST);
	glColor4f(0, 0, 0, 0);
}

void render(int width, int height) {
	glViewport(0, 0, (GLsizei)width, (GLsizei)height);

	glUniform1f(time_loc, current_time);
	glUniform2f(res_loc, width, height);

	glUniform1i(param0_loc, param0);
	glUniform1i(param1_loc, param1);
	glUniform1i(param2_loc, param2);

	glBegin(GL_QUADS);
	glVertex2f(-1, -1);
	glVertex2f(1, -1);
	glVertex2f(1, 1);
	glVertex2f(-1, 1);
	glEnd();
}