-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
282 lines (250 loc) · 5.73 KB
/
main.cpp
File metadata and controls
282 lines (250 loc) · 5.73 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*a Documentation
Example
./prog -i images/IMG_1664.JPG --filter='glsl:intensity_from_rgb(0,1)' --filter='glsl:gauss_x9(1,2)' --filter='glsl:gauss_y9(2,1)' --filter='glsl:harris(1,2)' --filter='find:a(2)' --filter='corr:correlation_copy_shader(0,3)'
*/
/*a Includes
*/
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "key_value.h"
#include "texture.h"
#include "shader.h"
#include "filter.h"
/*a Defines
*/
/*a Types
*/
/*t c_main
*/
class c_main
{
public:
c_main(void);
~c_main();
void check_sdl_error(void);
int init(void);
void exit(void);
int create_window(void);
SDL_Window *window;
SDL_GLContext glContext;
};
/*a Helper functions
*/
/*f gl_get_errors
*/
int gl_get_errors(const char *msg)
{
GLenum err;
int num_errors;
num_errors = 0;
while ((err = glGetError()) != GL_NO_ERROR) {
fprintf(stderr,"OpenGL error %s : %d\n", msg, err);
num_errors++;
}
return num_errors;
}
/*a Main methods
*/
/*f c_main constructor
*/
c_main::c_main(void)
{
window = NULL;
}
/*f c_main::check_sdl_error
*/
void
c_main::check_sdl_error(void)
{
const char *error;
error = SDL_GetError();
if (*error != '\0') {
fprintf(stderr,"SDL Error: %s\n", error);
SDL_ClearError();
}
}
/*f c_main::init
*/
int c_main::init(void)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
return 0;
}
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
check_sdl_error();
fprintf(stderr,"Main Init() completed\n");
return 1;
}
/*f c_main::exit
*/
void
c_main::exit(void)
{
if (window) {
SDL_DestroyWindow(window);
window = NULL;
}
SDL_Quit();
}
/*f c_main::create_window
*/
int
c_main::create_window()
{
int flags;
int width;
int height;
width = 64;
height = 64;
flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
window = SDL_CreateWindow("OpenGL Test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,width, height, flags);
check_sdl_error();
if (!window) {
return 0;
}
glContext = SDL_GL_CreateContext(window);
if (!glContext) {
return 0;
}
if (1) {
int major, minor;
SDL_GL_GetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, &major );
SDL_GL_GetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, &minor );
check_sdl_error();
fprintf(stderr, "Using OpenGL version %d.%d\n",major,minor);
}
return 1;
}
/*a Options
*/
/*v long_options
*/
static struct option long_options[] =
{
{"filter", required_argument, 0, 'f'},
{"infile", required_argument, 0, 'i'},
{"textures", required_argument, 0, 'n'},
{0, 0, 0, 0}
};
/*t t_option_list
*/
typedef struct
{
int num;
const char *strings[256];
} t_option_list;
/*t t_options
*/
typedef struct
{
int num_textures;
t_option_list images;
t_option_list filters;
} t_options;
/*f option_add_to_list
*/
static void option_add_to_list(t_option_list *option_list, const char *string)
{
option_list->strings[option_list->num++] = string;
}
/*f get_options
*/
static int get_options(int argc, char **argv, t_options *options)
{
int c;
options->num_textures = 0;
options->images.num=0;
options->filters.num=0;
while (1)
{
int option_index = 0;
c = getopt_long (argc, argv, "f:i:n:",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'f':
option_add_to_list(&options->filters, optarg);
break;
case 'i':
option_add_to_list(&options->images, optarg);
break;
case 'n':
options->num_textures = atoi(optarg);
break;
default:
break;
}
}
return 1;
}
/*a Toplevel
*/
/*f main
*/
int main(int argc,char *argv[])
{
int i;
c_main *m;
c_filter *filters[256];
t_options options;
t_exec_context ec;
ec.use_ids = 1;
m = new c_main();
if (m->init()==0) {
fprintf(stderr,"Initializtion failed\n");
return 4;
}
if (!m->create_window()) {
fprintf(stderr,"Create window failed\n");
return 4;
}
shader_init();
if (get_options(argc, argv, &options)==0) {
return 4;
}
for (int i=0; i<options.filters.num; i++) {
filters[i] = filter_from_string(options.filters.strings[i]);
}
int failures=0;
for (int i=0; i<options.filters.num; i++) {
if (!filters[i]) {
failures++;
continue;
}
if (filters[i]->parse_error) {
fprintf(stderr, "Filter '%s' parse error: %s\n", options.filters.strings[i], filters[i]->parse_error);
failures++;
continue;
}
if (filters[i]->compile()!=0) failures++;
}
if (failures>0) {
exit(4);
}
texture_draw_init();
for (i=0; i<options.images.num; i++) {
ec.textures[i] = texture_load(options.images.strings[i], GL_RGB);
}
for (i=options.images.num; i<options.images.num+options.num_textures; i++) {
ec.textures[i] = texture_create(1024, 1024);
}
fprintf(stderr,"Made %d textures\n", i);
ec.points = NULL;
for (int i=0; i<options.filters.num; i++) {
fprintf(stderr, "Execute '%s'\n", options.filters.strings[i]);
filters[i]->execute(&ec);
}
m->exit();
return 0;
}