Skip to content

Commit b02bfcb

Browse files
author
Tiffany Bennett
committed
Compile under VS
1 parent 57e55c4 commit b02bfcb

File tree

13 files changed

+135
-103
lines changed

13 files changed

+135
-103
lines changed

IntenseLogic

src/Demo.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#include <fenv.h>
88
#include <vector>
99
#include <string>
10+
#include <memory>
1011

1112
#include "tgl/tgl.h"
1213

1314
extern "C" {
1415
#include "util/logger.h"
1516
#include "util/log.h"
16-
#include "util/version.h"
1717
#include "util/opt.h"
1818
#include "graphics/graphics.h"
1919
}
@@ -32,7 +32,6 @@ const struct {
3232
const char *l, *h;
3333
} help[] = {
3434
{NO_ARG, 'h', "help", "Prints this message and exits"},
35-
{NO_ARG, 'v', "version", "Prints the version and exits"},
3635
{REQUIRED, 'd', "data", "Adds a directory to look for data files"},
3736
{REQUIRED, 's', "shaders", "Adds a directory to look for GLSL shaders"},
3837
{REQUIRED, 'f', "shader", "ShaderToy demo: Select shader to load"},
@@ -50,11 +49,10 @@ void demoLoad(int argc, char **argv)
5049

5150
for (i = 0; main_opts && i < main_opts->args.length; i++) {
5251
il_opt *opt = &main_opts->args.data[i];
53-
char *arg = strndup(opt->arg.str, opt->arg.len);
52+
std::string arg(opt->arg.str, opt->arg.len);
5453
#define option(s, l) if (il_string_cmp(opt->name, il_string_new(const_cast<char*>(s))) || \
5554
il_string_cmp(opt->name, il_string_new(const_cast<char*>(l))))
5655
option("h", "help") {
57-
printf("IntenseLogic %s\n", il_version);
5856
printf("Usage: %s [OPTIONS]\n\n", argv[0]);
5957
printf("Options:\n");
6058
for (i = 0; help[i].l; i++) {
@@ -78,46 +76,38 @@ void demoLoad(int argc, char **argv)
7876
}
7977
exit(0);
8078
}
81-
option("v", "version") {
82-
printf("IntenseLogic %s\n", il_version);
83-
printf("Commit: %s\n", il_commit);
84-
printf("Built: %s\n", il_build_date);
85-
exit(0);
86-
}
8779
option("d", "data") {
88-
ilA_adddir(&demo_fs, arg, -1);
80+
ilA_adddir(&demo_fs, arg.c_str(), -1);
8981
}
9082
option("s", "shaders") {
91-
ilG_shaders_addPath(arg);
83+
ilG_shaders_addPath(arg.c_str());
9284
}
9385
option("f", "shader") {
94-
demo_shader = arg;
95-
continue; // don't free arg
86+
demo_shader = std::move(arg);
9687
}
9788
option("", "fpe") {
89+
#ifdef _WIN32
90+
_controlfp(_EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW, _MCW_EM);
91+
#else
9892
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
93+
#endif
9994
il_log("Floating point exceptions enabled");
10095
}
101-
free(arg);
10296
}
10397

10498
ilG_shaders_addPath("shaders");
10599
ilG_shaders_addPath("IntenseLogic/shaders");
106100

107101
il_log("Initializing engine.");
108-
il_log("IntenseLogic %s", il_version);
109-
il_log("IL Commit: %s", il_commit);
110-
il_log("Built %s", il_build_date);
111-
112102
il_load_ilgraphics();
113103
}
114104

115105
struct DebugGroupStack {
116106
vector<string> entries;
117107
};
118108

119-
static GLvoid error_cb(GLenum esource, GLenum etype, GLuint id, GLenum eseverity,
120-
GLsizei length, const GLchar* message, const GLvoid* user)
109+
static GLvoid APIENTRY error_cb(GLenum esource, GLenum etype, GLuint id, GLenum eseverity,
110+
GLsizei length, const GLchar* message, const GLvoid* user)
121111
{
122112
auto &stack = *reinterpret_cast<DebugGroupStack*>(const_cast<void*>(user));
123113
const char *ssource;
@@ -228,5 +218,38 @@ Window createWindow(const char *title, unsigned msaa)
228218
return window;
229219
}
230220

221+
#ifdef _WIN32
222+
223+
int main(int argc, char **argv);
224+
225+
int CALLBACK WinMain(
226+
_In_ HINSTANCE hInstance,
227+
_In_ HINSTANCE hPrevInstance,
228+
_In_ LPSTR lpCmdLine,
229+
_In_ int nCmdShow)
230+
{
231+
LPWSTR lpwCmdLine = NULL;
232+
int len = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, lpCmdLine, -1, lpwCmdLine, 0);
233+
lpwCmdLine = new WCHAR[len];
234+
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, lpCmdLine, -1, lpwCmdLine, len);
235+
int argc;
236+
LPWSTR *argvw = CommandLineToArgvW(lpwCmdLine, &argc);
237+
delete[] lpwCmdLine;
238+
LPSTR *argv = new LPSTR[argc];
239+
for (int i = 0; i < argc; i++) {
240+
int len = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR, argvw[i], -1, argv[i], 0, NULL, NULL);
241+
argv[i] = new CHAR[len + 1];
242+
argv[i][len] = 0;
243+
WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS | WC_COMPOSITECHECK | WC_DEFAULTCHAR, argvw[i], -1, argv[i], len, NULL, NULL);
244+
}
245+
LocalFree(argvw);
246+
main(argc, argv);
247+
for (int i = 0; i < argc; i++) {
248+
delete[] argv[i];
249+
}
250+
}
251+
252+
#endif
253+
231254
ilA_fs demo_fs;
232-
const char *demo_shader;
255+
std::string demo_shader;

src/Demo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <SDL.h>
55
#include <utility>
6+
#include <string>
67

78
#include "tgl/tgl.h"
89

@@ -32,6 +33,6 @@ struct Window {
3233
Window createWindow(const char *title, unsigned msaa = 0);
3334

3435
extern ilA_fs demo_fs;
35-
extern const char *demo_shader;
36+
extern std::string demo_shader;
3637

3738
#endif

src/Graphics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ bool Graphics::init(const Flags &flags)
3131
}
3232
initialized = true;
3333

34-
ilG_renderman_setup(rm, flags.msaa, flags.hdr);
34+
ilG_renderman_setup(rm, flags.msaa != 0, flags.hdr);
3535
ilG_renderman_resize(rm, 800, 600);
3636
glClampColor(GL_CLAMP_READ_COLOR, GL_FALSE);
3737

@@ -76,17 +76,17 @@ bool Graphics::init(const Flags &flags)
7676
::free(error);
7777
return false;
7878
}
79-
if (!ilG_lighting_build(&sun, rm, &ico, ILG_SUN, flags.msaa, &error)) {
79+
if (!ilG_lighting_build(&sun, rm, &ico, ILG_SUN, flags.msaa != 0, &error)) {
8080
il_error("sunlighting: %s", error);
8181
::free(error);
8282
return false;
8383
}
84-
if (!ilG_lighting_build(&point, rm, &ico, ILG_POINT, flags.msaa, &error)) {
84+
if (!ilG_lighting_build(&point, rm, &ico, ILG_POINT, flags.msaa != 0, &error)) {
8585
il_error("lighting: %s", error);
8686
::free(error);
8787
return false;
8888
}
89-
if (!ilG_tonemapper_build(&tonemapper, rm, flags.msaa, &error)) {
89+
if (!ilG_tonemapper_build(&tonemapper, rm, flags.msaa != 0, &error)) {
9090
il_error("tonemapper: %s", error);
9191
::free(error);
9292
return false;

src/Graphics.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ struct State {
1616
bool mouse_grab = false;
1717
bool vsync = true;
1818
il_mat projection;
19-
float fov = M_PI / 4;
20-
float zmin = 0.5, zfar = 1024.0;
21-
il_vec3 ambient_col = il_vec3_new(.2,.2,.2);
22-
float exposure = 1.0, gamma = 1.0;
19+
float fov = float(M_PI) / 4.f;
20+
float zmin = 0.5f, zfar = 1024.f;
21+
il_vec3 ambient_col = il_vec3_new(.2f,.2f,.2f);
22+
float exposure = 1.f, gamma = 1.f;
2323

2424
unsigned *sunlight_locs = nullptr;
2525
ilG_light *sunlight_lights = nullptr;

src/bouncing-lights/bouncinglights.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include <utility>
1010
#include <ctime>
1111
#include <math.h>
12-
13-
#include <experimental/optional>
12+
#include <random>
1413

1514
#include "tgl/tgl.h"
1615
#include "debugdraw.hpp"
@@ -38,11 +37,6 @@ extern "C" {
3837

3938
const btScalar arenaWidth = 128;
4039

41-
float rand_float(unsigned *seedp)
42-
{
43-
return (float)rand_r(seedp) / RAND_MAX;
44-
}
45-
4640
struct Scene : public Drawable {
4741
Scene(BulletSpace &space)
4842
: space(space) {}
@@ -51,7 +45,7 @@ struct Scene : public Drawable {
5145
vector<BulletSpace::BodyID> bodies;
5246
vector<il_vec3> colors;
5347
vector<ilG_light> lights;
54-
experimental::optional<BulletSpace::BodyID> heightmap_body;
48+
BulletSpace::BodyID heightmap_body = BulletSpace::BodyID(0);
5549
ilG_heightmap heightmap;
5650
BallRenderer ball;
5751
btSphereShape sphere_shape = btSphereShape(1);
@@ -62,13 +56,13 @@ struct Scene : public Drawable {
6256
btStaticPlaneShape(btVector3( 0, 0, -1), 1)
6357
};
6458
btDefaultMotionState ground_motion_state[4], heightmap_motion_state;
65-
experimental::optional<btHeightfieldTerrainShape> heightmap_shape;
59+
btHeightfieldTerrainShape *heightmap_shape;
6660

6761
void draw(Graphics &graphics) override {
6862
(void)graphics;
6963
il_mat hmvp, himt;
70-
space.objmats(&hmvp, &*heightmap_body, ILG_MVP, 1);
71-
space.objmats(&himt, &*heightmap_body, ILG_IMT, 1);
64+
space.objmats(&hmvp, &heightmap_body, ILG_MVP, 1);
65+
space.objmats(&himt, &heightmap_body, ILG_IMT, 1);
7266
ilG_heightmap_draw(&heightmap, hmvp, himt);
7367

7468
vector<il_mat> mvp, imt;
@@ -112,17 +106,17 @@ struct Scene : public Drawable {
112106
// Physics
113107
/////////////////////
114108
const unsigned height = 50;
115-
heightmap_shape = btHeightfieldTerrainShape(hm.width, hm.height, hm.data, height/255.f, 0,
116-
height, 1, PHY_UCHAR, false);
109+
heightmap_shape = new btHeightfieldTerrainShape(hm.width, hm.height, hm.data, height/255.f, 0,
110+
height, 1, PHY_UCHAR, false);
117111
heightmap_shape->setLocalScaling(btVector3(arenaWidth/hm.width, 1, arenaWidth/hm.height));
118112
btTransform trans = btTransform(btQuaternion(0,0,0,1),
119113
btVector3(arenaWidth/2, height/2, arenaWidth/2));
120114
heightmap_motion_state = trans;
121115
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI
122116
(0, &heightmap_motion_state, &*heightmap_shape, btVector3(0,0,0));
123117
heightmap_body = space.add(groundRigidBodyCI);
124-
space.getBody(*heightmap_body).setRestitution(1.0);
125-
space.setBodyScale(*heightmap_body, il_vec3_new(128, 50, 128));
118+
space.getBody(heightmap_body).setRestitution(1.0);
119+
space.setBodyScale(heightmap_body, il_vec3_new(128, 50, 128));
126120
// Rendering
127121
///////////////////////
128122
ilA_img norm;
@@ -164,12 +158,16 @@ struct Scene : public Drawable {
164158

165159
void populate(size_t count) {
166160
unsigned seed;
161+
std::default_random_engine gen;
162+
std::uniform_int_distribution<> mod128(0, 127);
163+
std::uniform_int_distribution<> from50to75(50, 75);
164+
std::uniform_real_distribution<> unit(0.f, 1.f);
167165
for (size_t i = 0; i < count; i++) {
168166
// Physics body
169-
btVector3 position
170-
(rand_r(&seed) % 128,
171-
rand_r(&seed) % 32 + 50,
172-
rand_r(&seed) % 128);
167+
btVector3 position(
168+
mod128(gen),
169+
from50to75(gen),
170+
mod128(gen));
173171
auto state = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), position));
174172
float mass = 1.f;
175173
btVector3 inertia(0,0,0);
@@ -179,11 +177,11 @@ struct Scene : public Drawable {
179177
space.getBody(id).setRestitution(1.0);
180178

181179
// Color
182-
float brightness = rand_float(&seed) + 1;
180+
float brightness = unit(gen) + 1;
183181
auto col = il_vec3_new(
184-
rand_float(&seed) * brightness,
185-
rand_float(&seed) * brightness,
186-
rand_float(&seed) * brightness);
182+
unit(gen) * brightness,
183+
unit(gen) * brightness,
184+
unit(gen) * brightness);
187185

188186
// Light
189187
ilG_light light;

src/bouncing-lights/bulletspace.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ struct BulletSpace {
2626

2727
public:
2828
class BodyID {
29-
BodyID(unsigned id) : id(id) {}
3029
unsigned id;
3130
friend BulletSpace;
31+
3232
public:
33-
unsigned value() const
34-
{
33+
BodyID(unsigned id) : id(id) {}
34+
35+
unsigned value() const {
3536
return id;
3637
}
3738
};

src/box/box.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <SDL.h>
22
#include <time.h>
3-
#include <sys/time.h>
43
#include <math.h>
4+
#include <chrono>
55

66
#include "Demo.h"
77

@@ -63,6 +63,8 @@ static const float cube[] = {
6363
1.0, -1.0, 1.0,
6464
};
6565

66+
using namespace std;
67+
6668
int main(int argc, char **argv)
6769
{
6870
demoLoad(argc, argv);
@@ -100,7 +102,7 @@ int main(int argc, char **argv)
100102

101103
ilG_floatspace fs;
102104
ilG_floatspace_init(&fs, 1);
103-
fs.projection = il_mat_perspective(M_PI / 4.0, 4.0/3, .5, 200);
105+
fs.projection = il_mat_perspective(float(M_PI / 4.f), 4.f/3, .5f, 200.f);
104106

105107
il_pos_setPosition(&fs.camera, il_vec3_new(0, 0, 5));
106108

@@ -111,6 +113,10 @@ int main(int argc, char **argv)
111113
glBindBuffer(GL_ARRAY_BUFFER, vbo);
112114
glEnable(GL_DEPTH_TEST);
113115

116+
typedef chrono::steady_clock clock;
117+
typedef chrono::duration<double> duration;
118+
119+
clock::time_point start = clock::now();
114120
while (1) {
115121
SDL_Event ev;
116122
while (SDL_PollEvent(&ev)) {
@@ -124,15 +130,14 @@ int main(int argc, char **argv)
124130
}
125131
}
126132

127-
struct timeval ts;
128-
gettimeofday(&ts, NULL);
129-
int secs = 5;
130-
float delta = ((float)(ts.tv_sec%secs) + ts.tv_usec / 1000000.0) / secs;
133+
duration delta = clock::now() - start;
131134
il_vec3 v;
132-
v.x = sinf(delta * M_PI * 2) * 5;
133-
v.y = 0;
134-
v.z = cosf(delta * M_PI * 2) * 5;
135-
il_quat q = il_quat_fromAxisAngle(0, 1, 0, delta * M_PI * 2);
135+
const float secs = 5.f;
136+
const float dist = 5.f;
137+
v.x = float(sin(delta.count() * M_PI * 2.0 / secs) * dist);
138+
v.y = 0.f;
139+
v.z = float(cos(delta.count() * M_PI * 2.0 / secs) * dist);
140+
il_quat q = il_quat_fromAxisAngle(0, 1, 0, float(delta.count() * M_PI * 2 / secs));
136141
il_pos_setPosition(&fs.camera, v);
137142
il_pos_setRotation(&fs.camera, q);
138143

0 commit comments

Comments
 (0)