Selaa lähdekoodia

Experiment with SDL and tracy (which is really neat!)

Luna Stadler 4 vuotta sitten
vanhempi
commit
cbacf371c9
4 muutettua tiedostoa jossa 313 lisäystä ja 0 poistoa
  1. 2 0
      c/tracy/.gitignore
  2. 11 0
      c/tracy/Makefile
  3. 99 0
      c/tracy/load_shaders.cpp
  4. 201 0
      c/tracy/main.c

+ 2 - 0
c/tracy/.gitignore

@ -0,0 +1,2 @@
1
/tracy
2
*.o

+ 11 - 0
c/tracy/Makefile

@ -0,0 +1,11 @@
1
CFLAGS = -Wall -g -lGL $(shell sdl2-config --cflags --libs) -ldl -lstdc++ -lm
2
TRACY_FLAGS = -DTRACY_ENABLE=1
3
4
main: main.c tracy.o load_shaders.o
5
	gcc $(CFLAGS) $(TRACY_FLAGS) load_shaders.o tracy.o main.c -o main
6
7
load_shaders.o: load_shaders.cpp
8
	g++ -c $(TRACY_FLAGS) -o load_shaders.o load_shaders.cpp
9
10
tracy.o: tracy/TracyClient.cpp
11
	g++ -c $(TRACY_FLAGS) -o tracy.o tracy/TracyClient.cpp

+ 99 - 0
c/tracy/load_shaders.cpp

@ -0,0 +1,99 @@
1
#include <stdio.h>
2
#include <string>
3
#include <vector>
4
#include <iostream>
5
#include <fstream>
6
#include <algorithm>
7
#include <sstream>
8
using namespace std;
9
10
#include <GL/gl.h>
11
#include <GL/glu.h>
12
#include <GL/glext.h>
13
14
GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path) {
15
	// Create the shaders
16
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
17
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
18
19
	// Read the Vertex Shader code from the file
20
	std::string VertexShaderCode;
21
	std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
22
	if(VertexShaderStream.is_open()){
23
		std::stringstream sstr;
24
		sstr << VertexShaderStream.rdbuf();
25
		VertexShaderCode = sstr.str();
26
		VertexShaderStream.close();
27
	}else{
28
		printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
29
		getchar();
30
		return 0;
31
	}
32
33
	// Read the Fragment Shader code from the file
34
	std::string FragmentShaderCode;
35
	std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
36
	if(FragmentShaderStream.is_open()){
37
		std::stringstream sstr;
38
		sstr << FragmentShaderStream.rdbuf();
39
		FragmentShaderCode = sstr.str();
40
		FragmentShaderStream.close();
41
	}
42
43
	GLint Result = GL_FALSE;
44
	int InfoLogLength;
45
46
	// Compile Vertex Shader
47
	printf("Compiling shader : %s\n", vertex_file_path);
48
	char const * VertexSourcePointer = VertexShaderCode.c_str();
49
	glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
50
	glCompileShader(VertexShaderID);
51
52
	// Check Vertex Shader
53
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
54
	glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
55
	if ( InfoLogLength > 0 ){
56
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
57
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
58
		printf("%s\n", &VertexShaderErrorMessage[0]);
59
	}
60
61
	// Compile Fragment Shader
62
	printf("Compiling shader : %s\n", fragment_file_path);
63
	char const * FragmentSourcePointer = FragmentShaderCode.c_str();
64
	glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
65
	glCompileShader(FragmentShaderID);
66
67
	// Check Fragment Shader
68
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
69
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
70
	if ( InfoLogLength > 0 ){
71
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
72
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
73
		printf("%s\n", &FragmentShaderErrorMessage[0]);
74
	}
75
76
	// Link the program
77
	printf("Linking program\n");
78
	GLuint ProgramID = glCreateProgram();
79
	glAttachShader(ProgramID, VertexShaderID);
80
	glAttachShader(ProgramID, FragmentShaderID);
81
	glLinkProgram(ProgramID);
82
83
	// Check the program
84
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
85
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
86
	if ( InfoLogLength > 0 ){
87
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
88
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
89
		printf("%s\n", &ProgramErrorMessage[0]);
90
	}
91
92
	glDetachShader(ProgramID, VertexShaderID);
93
	glDetachShader(ProgramID, FragmentShaderID);
94
95
	glDeleteShader(VertexShaderID);
96
	glDeleteShader(FragmentShaderID);
97
98
	return ProgramID;
99
}

+ 201 - 0
c/tracy/main.c

@ -0,0 +1,201 @@
1
// Original SDL2/OpenGL code from https://gist.github.com/jordandee/94b187bcc51df9528a2f
2
#include <stdio.h>
3
#include <stdbool.h>
4
#include <stdint.h>
5
#include <assert.h>
6
#include <SDL2/SDL.h>
7
#include <SDL2/SDL_opengl.h>
8
#include <GL/gl.h>
9
10
#include "tracy/TracyC.h"
11
12
#define WINDOW_WIDTH  1000
13
#define WINDOW_HEIGHT 1000
14
15
static const GLfloat g_vertex_buffer_data[] = {
16
	-1.0f,-1.0f,-1.0f, // triangle 1 : begin
17
	-1.0f,-1.0f, 1.0f,
18
	-1.0f, 1.0f, 1.0f, // triangle 1 : end
19
	1.0f, 1.0f,-1.0f, // triangle 2 : begin
20
	-1.0f,-1.0f,-1.0f,
21
	-1.0f, 1.0f,-1.0f, // triangle 2 : end
22
	1.0f,-1.0f, 1.0f,
23
	-1.0f,-1.0f,-1.0f,
24
	1.0f,-1.0f,-1.0f,
25
	1.0f, 1.0f,-1.0f,
26
	1.0f,-1.0f,-1.0f,
27
	-1.0f,-1.0f,-1.0f,
28
	-1.0f,-1.0f,-1.0f,
29
	-1.0f, 1.0f, 1.0f,
30
	-1.0f, 1.0f,-1.0f,
31
	1.0f,-1.0f, 1.0f,
32
	-1.0f,-1.0f, 1.0f,
33
	-1.0f,-1.0f,-1.0f,
34
	-1.0f, 1.0f, 1.0f,
35
	-1.0f,-1.0f, 1.0f,
36
	1.0f,-1.0f, 1.0f,
37
	1.0f, 1.0f, 1.0f,
38
	1.0f,-1.0f,-1.0f,
39
	1.0f, 1.0f,-1.0f,
40
	1.0f,-1.0f,-1.0f,
41
	1.0f, 1.0f, 1.0f,
42
	1.0f,-1.0f, 1.0f,
43
	1.0f, 1.0f, 1.0f,
44
	1.0f, 1.0f,-1.0f,
45
	-1.0f, 1.0f,-1.0f,
46
	1.0f, 1.0f, 1.0f,
47
	-1.0f, 1.0f,-1.0f,
48
	-1.0f, 1.0f, 1.0f,
49
	1.0f, 1.0f, 1.0f,
50
	-1.0f, 1.0f, 1.0f,
51
	1.0f,-1.0f, 1.0f
52
};
53
54
// One color for each vertex. They were generated randomly.
55
static const GLfloat g_color_buffer_data[] = {
56
	0.583f,  0.771f,  0.014f,
57
	0.609f,  0.115f,  0.436f,
58
	0.327f,  0.483f,  0.844f,
59
	0.822f,  0.569f,  0.201f,
60
	0.435f,  0.602f,  0.223f,
61
	0.310f,  0.747f,  0.185f,
62
	0.597f,  0.770f,  0.761f,
63
	0.559f,  0.436f,  0.730f,
64
	0.359f,  0.583f,  0.152f,
65
	0.483f,  0.596f,  0.789f,
66
	0.559f,  0.861f,  0.639f,
67
	0.195f,  0.548f,  0.859f,
68
	0.014f,  0.184f,  0.576f,
69
	0.771f,  0.328f,  0.970f,
70
	0.406f,  0.615f,  0.116f,
71
	0.676f,  0.977f,  0.133f,
72
	0.971f,  0.572f,  0.833f,
73
	0.140f,  0.616f,  0.489f,
74
	0.997f,  0.513f,  0.064f,
75
	0.945f,  0.719f,  0.592f,
76
	0.543f,  0.021f,  0.978f,
77
	0.279f,  0.317f,  0.505f,
78
	0.167f,  0.620f,  0.077f,
79
	0.347f,  0.857f,  0.137f,
80
	0.055f,  0.953f,  0.042f,
81
	0.714f,  0.505f,  0.345f,
82
	0.783f,  0.290f,  0.734f,
83
	0.722f,  0.645f,  0.174f,
84
	0.302f,  0.455f,  0.848f,
85
	0.225f,  0.587f,  0.040f,
86
	0.517f,  0.713f,  0.338f,
87
	0.053f,  0.959f,  0.120f,
88
	0.393f,  0.621f,  0.362f,
89
	0.673f,  0.211f,  0.457f,
90
	0.820f,  0.883f,  0.371f,
91
	0.982f,  0.099f,  0.879f
92
};
93
94
int main (int ArgCount, char **Args) {
95
	TracyCSetThreadName("main");
96
97
	float r = 1.0;
98
	float b = 1.0;
99
100
	uint32_t window_flags = SDL_WINDOW_OPENGL;
101
	SDL_Window *window = SDL_CreateWindow("Hello, Tracy!", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, window_flags);
102
	assert(window);
103
	SDL_GL_CreateContext(window);
104
105
	int32_t Running = 1;
106
	int32_t FullScreen = 0;
107
	while (Running) {
108
		TracyCZoneN(input, "input", true);
109
		SDL_Event Event;
110
		while (SDL_PollEvent(&Event)) {
111
			if (Event.type == SDL_KEYDOWN) {
112
				switch (Event.key.keysym.sym) {
113
					case SDLK_ESCAPE:
114
						Running = 0;
115
						break;
116
					case 'f':
117
						FullScreen = !FullScreen;
118
						if (FullScreen) {
119
							SDL_SetWindowFullscreen(window, window_flags | SDL_WINDOW_FULLSCREEN_DESKTOP);
120
						} else {
121
							SDL_SetWindowFullscreen(window, window_flags);
122
						}
123
						break;
124
					default:
125
						break;
126
				}
127
			} else if (Event.type == SDL_MOUSEMOTION) {
128
				r = sin(Event.motion.x / (float)WINDOW_WIDTH);
129
				b = cos(Event.motion.y / (float)WINDOW_HEIGHT);
130
			} else if (Event.type == SDL_QUIT) {
131
				Running = 0;
132
			}
133
		}
134
		TracyCZoneEnd(input);
135
136
		/*r += 0.05;
137
		b += 0.05;
138
		int x, y;
139
		SDL_GetMouseState(&x, &y);
140
		r = x / (float)WINDOW_WIDTH;
141
		b = y / (float)WINDOW_HEIGHT;*/
142
143
		TracyCZoneN(draw, "draw", true);
144
		glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
145
		glClearColor(r, 0.f, b, 0.f);
146
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
147
148
		GLuint VertexArrayID;
149
		glGenVertexArrays(1, &VertexArrayID);
150
		glBindVertexArray(VertexArrayID);
151
152
		// This will identify our vertex buffer
153
		GLuint vertexbuffer;
154
		// Generate 1 buffer, put the resulting identifier in vertexbuffer
155
		glGenBuffers(1, &vertexbuffer);
156
		// The following commands will talk about our 'vertexbuffer' buffer
157
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
158
		// Give our vertices to OpenGL.
159
		glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
160
161
		glEnableVertexAttribArray(0);
162
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
163
		glVertexAttribPointer(
164
				0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
165
				3,                  // size
166
				GL_FLOAT,           // type
167
				GL_FALSE,           // normalized?
168
				0,                  // stride
169
				(void*)0            // array buffer offset
170
				);
171
172
		GLuint colorbuffer;
173
		glGenBuffers(1, &colorbuffer);
174
		glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
175
		glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
176
177
		// 2nd attribute buffer : colors
178
		glEnableVertexAttribArray(1);
179
		glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
180
		glVertexAttribPointer(
181
				1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
182
				3,                                // size
183
				GL_FLOAT,                         // type
184
				GL_FALSE,                         // normalized?
185
				0,                                // stride
186
				(void*)0                          // array buffer offset
187
				);
188
189
		glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
190
		glDisableVertexAttribArray(0);
191
192
		TracyCZoneEnd(draw);
193
194
		TracyCZoneN(swap_window, "swap_window", true);
195
		SDL_GL_SwapWindow(window);
196
		TracyCZoneEnd(swap_window);
197
198
		TracyCFrameMark;
199
	}
200
	return 0;
201
}