Skip to content

Commit 3b940bd

Browse files
author
Tiffany Bennett
committed
Add new helper functions
1 parent f64cf88 commit 3b940bd

File tree

6 files changed

+183
-147
lines changed

6 files changed

+183
-147
lines changed

src/main.c renamed to src/Demo.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
#include <string.h>
2-
#include <stdlib.h>
3-
#include <stdio.h>
4-
#include <string.h>
1+
#include "Demo.h"
52

3+
#include <cstring>
4+
#include <cstdlib>
5+
#include <cstdio>
6+
#include <cstring>
7+
#include <fenv.h>
8+
9+
#include "tgl/tgl.h"
10+
11+
extern "C" {
612
#include "util/log.h"
713
#include "util/version.h"
814
#include "util/opt.h"
9-
#include "asset/node.h"
1015
#include "graphics/graphics.h"
16+
}
17+
18+
enum ArgType {
19+
NO_ARG,
20+
REQUIRED,
21+
OPTIONAL
22+
};
1123

1224
const struct {
13-
enum {
14-
NO_ARG,
15-
REQUIRED,
16-
OPTIONAL
17-
} arg;
25+
ArgType arg;
1826
char s;
1927
const char *l, *h;
2028
} help[] = {
@@ -23,30 +31,26 @@ const struct {
2331
{REQUIRED, 'd', "data", "Adds a directory to look for data files"},
2432
{REQUIRED, 's', "shaders", "Adds a directory to look for GLSL shaders"},
2533
{REQUIRED, 'f', "shader", "ShaderToy demo: Select shader to load"},
34+
{NO_ARG, 0, "fpe", "Enable trapping on floating point exceptions"},
2635
{NO_ARG, 0, NULL, NULL}
2736
};
2837

29-
ilA_fs demo_fs;
30-
const char *demo_shader;
31-
32-
void demo_start();
33-
34-
int main(int argc, char **argv)
38+
void demoLoad(int argc, char **argv)
3539
{
3640
size_t i;
3741
il_opts opts = il_opt_parse(argc, argv);
38-
il_modopts *main_opts = il_opts_lookup(&opts, "");
42+
il_modopts *main_opts = il_opts_lookup(&opts, const_cast<char*>(""));
3943

4044
ilA_adddir(&demo_fs, "assets", -1);
4145

4246
for (i = 0; main_opts && i < main_opts->args.length; i++) {
4347
il_opt *opt = &main_opts->args.data[i];
4448
char *arg = strndup(opt->arg.str, opt->arg.len);
45-
#define option(s, l) if (il_string_cmp(opt->name, il_string_new(s)) || il_string_cmp(opt->name, il_string_new(l)))
49+
#define option(s, l) if (il_string_cmp(opt->name, il_string_new(const_cast<char*>(s))) || \
50+
il_string_cmp(opt->name, il_string_new(const_cast<char*>(l))))
4651
option("h", "help") {
4752
printf("IntenseLogic %s\n", il_version);
4853
printf("Usage: %s [OPTIONS]\n\n", argv[0]);
49-
printf("Each module may have its own options, see relavent documentation for those.\n\n");
5054
printf("Options:\n");
5155
for (i = 0; help[i].l; i++) {
5256
static const char *const arg_strs[] = {
@@ -67,13 +71,13 @@ int main(int argc, char **argv)
6771
help[i].h
6872
);
6973
}
70-
return 0;
74+
exit(0);
7175
}
7276
option("v", "version") {
7377
printf("IntenseLogic %s\n", il_version);
7478
printf("Commit: %s\n", il_commit);
7579
printf("Built: %s\n", il_build_date);
76-
return 0;
80+
exit(0);
7781
}
7882
option("d", "data") {
7983
ilA_adddir(&demo_fs, arg, -1);
@@ -85,6 +89,10 @@ int main(int argc, char **argv)
8589
demo_shader = arg;
8690
continue; // don't free arg
8791
}
92+
option("", "fpe") {
93+
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
94+
il_log("Floating point exceptions enabled");
95+
}
8896
free(arg);
8997
}
9098

@@ -97,8 +105,43 @@ int main(int argc, char **argv)
97105
il_log("Built %s", il_build_date);
98106

99107
il_load_ilgraphics();
100-
demo_start();
101-
ilG_quit();
108+
}
109+
110+
Window createWindow(const char *title, unsigned msaa)
111+
{
112+
Window window;
113+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
114+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
115+
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
116+
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
117+
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
118+
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, msaa != 0);
119+
if (msaa) {
120+
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
121+
}
122+
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
123+
window.window = SDL_CreateWindow(
124+
title,
125+
SDL_WINDOWPOS_UNDEFINED,
126+
SDL_WINDOWPOS_UNDEFINED,
127+
800, 600,
128+
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
129+
if (!window.window) {
130+
il_error("SDL_CreateWindow: %s", SDL_GetError());
131+
exit(1);
132+
}
133+
window.context = SDL_GL_CreateContext(window.window);
134+
if (!window.context) {
135+
il_error("SDL_GL_CreateContext: %s", SDL_GetError());
136+
exit(1);
137+
}
138+
if (epoxy_gl_version() < 32) {
139+
il_error("Expected GL 3.2, got %u", epoxy_gl_version());
140+
exit(1);
141+
}
102142

103-
return 0;
143+
return window;
104144
}
145+
146+
ilA_fs demo_fs;
147+
const char *demo_shader;

src/Demo.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef DEMO_H
2+
#define DEMO_H
3+
4+
#include <SDL.h>
5+
#include <utility>
6+
7+
#include "tgl/tgl.h"
8+
9+
extern "C" {
10+
#include "asset/node.h"
11+
}
12+
13+
void demoLoad(int argc, char **argv);
14+
15+
struct Window {
16+
SDL_Window *window;
17+
SDL_GLContext context;
18+
19+
std::pair<int, int> resize() {
20+
int width, height;
21+
SDL_GetWindowSize(window, &width, &height);
22+
glViewport(0, 0, width, height);
23+
return std::make_pair(width, height);
24+
}
25+
void close() {
26+
SDL_DestroyWindow(window);
27+
}
28+
void swap() {
29+
SDL_GL_SwapWindow(window);
30+
}
31+
};
32+
Window createWindow(const char *title, unsigned msaa = 0);
33+
34+
extern ilA_fs demo_fs;
35+
extern const char *demo_shader;
36+
37+
#endif

src/helper.c

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/helper.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/quad/quad.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <SDL.h>
2+
#include <time.h>
3+
#include <sys/time.h>
4+
#include <math.h>
5+
6+
#include "Demo.h"
7+
8+
extern "C" {
9+
#include "graphics/arrayattrib.h"
10+
#include "graphics/context.h"
11+
#include "graphics/floatspace.h"
12+
#include "graphics/material.h"
13+
#include "tgl/tgl.h"
14+
#include "graphics/renderer.h"
15+
#include "math/matrix.h"
16+
#include "util/log.h"
17+
}
18+
19+
int main(int argc, char **argv)
20+
{
21+
demoLoad(argc, argv);
22+
Window window = createWindow("Simple Rainbow Quad");
23+
ilG_renderman rm[1];
24+
ilG_matid mat;
25+
tgl_quad quad;
26+
tgl_vao vao;
27+
28+
memset(rm, 0, sizeof(*rm));
29+
30+
char *error;
31+
ilG_material m;
32+
ilG_material_init(&m);
33+
ilG_material_name(&m, "Rainbow Quad Shader");
34+
ilG_material_arrayAttrib(&m, ILG_ARRATTR_POSITION, "in_Position");
35+
if (!ilG_renderman_addMaterialFromFile(rm, m, "id2d.vert", "rainbow2d.frag", &mat, &error)) {
36+
il_error("addMaterial: %s", error);
37+
free(error);
38+
return 1;
39+
}
40+
ilG_material *mptr = ilG_renderman_findMaterial(rm, mat);
41+
tgl_vao_init(&vao);
42+
tgl_vao_bind(&vao);
43+
tgl_quad_init(&quad, ILG_ARRATTR_POSITION);
44+
45+
ilG_material_bind(mptr);
46+
tgl_vao_bind(&vao);
47+
48+
while (1) {
49+
SDL_Event ev;
50+
while (SDL_PollEvent(&ev)) {
51+
switch (ev.type) {
52+
case SDL_QUIT:
53+
ilG_renderman_delMaterial(rm, mat);
54+
tgl_vao_free(&vao);
55+
tgl_quad_free(&quad);
56+
window.close();
57+
return 0;
58+
}
59+
}
60+
window.resize();
61+
tgl_quad_draw_once(&quad);
62+
window.swap();
63+
}
64+
}

0 commit comments

Comments
 (0)