summaryrefslogtreecommitdiff
path: root/src/scene01.c
blob: 527e81ac88ad1e403e7de3af62541b6683834ea4 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * demoscene-eo, an ASCII art demoscene written as a gift for Emmanuel Otton retirement 
 * Copyright (C) 2019  XXXXXX XXXXXXX <xxxxxx@xxxxxx.fr>
 *  
 * This file is part of demoscene-eo.
 *
 *  demoscene-eo 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.
 *
 *  demoscene-eo 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 demoscene-eo.  If not, see <http://www.gnu.org/licenses/>
 */
#include "scene01.h"

// Mostly inspired by CC0 https://gist.github.com/koute/7391344
typedef float t_mat4x4[16];

static inline void mat4x4_ortho( t_mat4x4 out, float left, float right, float bottom, float top, float znear, float zfar )
{
	#define T(a, b) (a * 4 + b)

	out[T(0,0)] = 2.0f / (right - left);
	out[T(0,1)] = 0.0f;
	out[T(0,2)] = 0.0f;
	out[T(0,3)] = 0.0f;

	out[T(1,1)] = 2.0f / (top - bottom);
	out[T(1,0)] = 0.0f;
	out[T(1,2)] = 0.0f;
	out[T(1,3)] = 0.0f;

	out[T(2,2)] = -2.0f / (zfar - znear);
	out[T(2,0)] = 0.0f;
	out[T(2,1)] = 0.0f;
	out[T(2,3)] = 0.0f;

	out[T(3,0)] = -(right + left) / (right - left);
	out[T(3,1)] = -(top + bottom) / (top - bottom);
	out[T(3,2)] = -(zfar + znear) / (zfar - znear);
	out[T(3,3)] = 1.0f;

	#undef T
}

static const char * vertex_shader =
	"#version 130\n"
	"in vec2 i_position;\n"
	"in vec4 i_color;\n"
	"out vec4 v_color;\n"
	"uniform mat4 u_projection_matrix;\n"
	"void main() {\n"
	"	v_color = i_color;\n"
	"	gl_Position = u_projection_matrix * vec4( i_position, 0.0, 1.0 );\n"
	"}\n";

static const char * fragment_shader =
	"#version 130\n"
	"in vec4 v_color;\n"
	"out vec4 o_color;\n"
	"void main() {\n"
	"	o_color = v_color;\n"
	"}\n";

typedef enum t_attrib_id
{
	attrib_position,
	attrib_color
} t_attrib_id;

int scene01_init_gl(graphical_env_t *ge, scene01_env_t *se) {
	GLuint vs, fs, program;

	vs = glCreateShader( GL_VERTEX_SHADER );
	fs = glCreateShader( GL_FRAGMENT_SHADER );

	int length = strlen( vertex_shader );
	glShaderSource( vs, 1, ( const GLchar ** )&vertex_shader, &length );
	glCompileShader( vs );

	GLint status;
	glGetShaderiv( vs, GL_COMPILE_STATUS, &status );
	if( status == GL_FALSE )
	{
		fprintf( stderr, "vertex shader compilation failed\n" );
		return 1;
	}

	length = strlen( fragment_shader );
	glShaderSource( fs, 1, ( const GLchar ** )&fragment_shader, &length );
	glCompileShader( fs );

	glGetShaderiv( fs, GL_COMPILE_STATUS, &status );
	if( status == GL_FALSE )
	{
		fprintf( stderr, "fragment shader compilation failed\n" );
		return 1;
	}

	program = glCreateProgram();
	glAttachShader( program, vs );
	glAttachShader( program, fs );

	glBindAttribLocation( program, attrib_position, "i_position" );
	glBindAttribLocation( program, attrib_color, "i_color" );
	glLinkProgram( program );

	glUseProgram( program );

	glDisable( GL_DEPTH_TEST );
	//glClearColor( 0.5, 0.0, 0.0, 0.0 );
	glClearColor( 0.5, 0.5, 0.0, 0.0 );
	glViewport( 0, 0, FBUF_W, FBUF_H );

	GLuint vbo;

	glGenVertexArrays( 1, &(se->vao) );
	glGenBuffers( 1, &vbo );
	glBindVertexArray( se->vao );
	glBindBuffer( GL_ARRAY_BUFFER, vbo );

	glEnableVertexAttribArray( attrib_position );
	glEnableVertexAttribArray( attrib_color );

	glVertexAttribPointer( attrib_color, 4, GL_FLOAT, GL_FALSE, sizeof( float ) * 6, 0 );
	glVertexAttribPointer( attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 6, ( void * )(4 * sizeof(float)) );

	const GLfloat g_vertex_buffer_data[] = {
	/*  R, G, B, A, X, Y  */
		1, 0, 0, 1, 0, 0,
		0, 1, 0, 1, FBUF_W, 0,
		0, 0, 1, 1, FBUF_W, FBUF_H,

		1, 0, 0, 1, 0, 0,
		0, 0, 1, 1, FBUF_W, FBUF_H,
		1, 1, 1, 1, 0, FBUF_H
	};

	glBufferData( GL_ARRAY_BUFFER, sizeof( g_vertex_buffer_data ), g_vertex_buffer_data, GL_STATIC_DRAW );

	t_mat4x4 projection_matrix;
	mat4x4_ortho( projection_matrix, 0.0f, (float)FBUF_W, (float)FBUF_H, 0.0f, 0.0f, 100.0f );
	glUniformMatrix4fv( glGetUniformLocation( program, "u_projection_matrix" ), 1, GL_FALSE, projection_matrix );
	return 0;
}

int scene01_init_sdl(graphical_env_t *ge, scene01_env_t *se) {
	return 0;
}

int scene01_init_caca(graphical_env_t *ge, scene01_env_t *se) {
	return 0;
}

void scene01_free_gl(graphical_env_t *ge, scene01_env_t *se) {
	//TODO
}

void scene01_free_sdl(graphical_env_t *ge, scene01_env_t *se) {
}

void scene01_free_caca(graphical_env_t *ge, scene01_env_t *se) {
}

int scene01_next_gl(graphical_env_t *ge, scene01_env_t *se) {
	// Shorthands
	caca_canvas_t *cv = ge->cv;
	int w = ge->w, h = ge->h;
	SDL_Renderer *r = ge->sdl_rndr;
	// Local vars
	int res;

	// https://gist.github.com/Twinklebear/8265888
	// https://forums.libsdl.org/viewtopic.php?p=51634

	// Render all the stuff on target texture
	SDL_SetRenderTarget(r, ge->sdl_target);

	glClear( GL_COLOR_BUFFER_BIT );
	glBindVertexArray( se->vao );
	glDrawArrays( GL_TRIANGLES, 0, 6 );

	// Copy the SDL screen to SDL debug window (and display it, not mandatory)
	SDL_SetRenderTarget(r, NULL);
	SDL_RenderCopy(r, ge->sdl_target, NULL, NULL);
	SDL_RenderPresent(r);
	
	// Download the rendered texture from videocard to main memory
	SDL_SetRenderTarget(r, ge->sdl_target);
	res = SDL_RenderReadPixels(r, NULL, 0, ge->raw_target, 256*4);

	// "convert" the raw pixel stream to ASCII art on caca canevas
	caca_set_dither_gamma(ge->d, 1.0);
	if ( res == 0 ) caca_dither_bitmap(cv, 0, 0, w, h, ge->d, ge->raw_target);

	return 0;
}

int scene01_next_sdl(graphical_env_t *ge, scene01_env_t *se) {
	return 0;
}

int scene01_next_caca(graphical_env_t *ge, scene01_env_t *se) {
	// Shorthands
	caca_canvas_t *cv = ge->cv;
	int w = ge->w, h = ge->h;
	Uint32 frame = ge->sc_framecount;

	caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
	caca_put_str(cv, (w-17)/2, h/2, "This is a message");

	if ( frame >= 300 ) {
		return 1;
	}
	return 0;
}