summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile.win3238
-rw-r--r--gl.cpp78
-rw-r--r--gl.h44
-rw-r--r--main.cpp31
-rw-r--r--scene.cpp18
-rw-r--r--scene.h2
-rw-r--r--terrain.cpp13
-rw-r--r--terrain.h1
9 files changed, 209 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore
index 30475e9..088042e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,5 @@
/dist
/Session.vim
/map
+*.dll
+*.exe
diff --git a/Makefile.win32 b/Makefile.win32
new file mode 100644
index 0000000..4b64070
--- /dev/null
+++ b/Makefile.win32
@@ -0,0 +1,38 @@
+ifeq ($(shell uname), Linux)
+ CXX=i486-mingw32-g++
+ LD=i486-mingw32-g++
+ ROOT=/usr/i486-mingw32
+else
+ CXX=g++
+ LD=g++
+ ROOT=C:/MinGW
+endif
+
+TARGET=foo.exe
+#OBJECTS=$(shell ls *.cpp | sed 's/cpp/o/')
+OBJECTS=gl.o gui.o main.o terrain.o scene.o shader.o tool.o vector.o video.o
+
+ifeq ($(shell uname), Linux)
+ OBJECTS+=noiseutils/noiseutils.o
+else
+ OBJECTS+=noiseutils\noiseutils.o
+endif
+
+#-D_GNU_SOURCE=1 -D_REENTRANT
+CPPFLAGS=-I$(ROOT)/include/SDL -I$(ROOT)/include/CEGUI/ -I$(ROOT)/include/freetype2 -Wall -g -mconsole #-mwin32
+LDFLAGS=-L$(ROOT)/lib -lCEGUIOpenGLRenderer -lCEGUIBase -lCEGUITGAImageCodec -lCEGUITinyXMLParser -lCEGUIFalagardWRBase -lCEGUISILLYImageCodec
+LDFLAGS+=-lSDL_image -lftgl -lfreetype -lz -lpcre -lopengl32 -lglu32 -lwinmm -lmingw32 -lSDLmain -lSDL -ljpeg -lstdc++ -lnoise
+
+ifeq ($(shell uname), Linux)
+ LDFLAGS+=-lboost_filesystem-mt-s -lboost_system-mt-s
+else
+ LDFLAGS+=-lboost_filesystem -lboost_system
+endif
+
+all: $(TARGET)
+
+$(TARGET): $(OBJECTS)
+ $(LD) -o $@ $^ $(LDFLAGS)
+
+clean:
+ del $(OBJECTS) $(TARGET)
diff --git a/gl.cpp b/gl.cpp
new file mode 100644
index 0000000..9cefa53
--- /dev/null
+++ b/gl.cpp
@@ -0,0 +1,78 @@
+#include "gl.h"
+
+#include <SDL.h>
+
+#ifdef WIN32
+
+void (APIENTRY *glGenBuffers)(GLsizei, GLuint*);
+void (APIENTRY *glDeleteBuffers)(GLsizei, GLuint*);
+void (APIENTRY *glBindBuffer)(GLenum, GLuint);
+void (APIENTRY *glBufferData)(GLenum, GLsizeiptr, const GLvoid*, GLenum);
+GLvoid* (APIENTRY *glMapBuffer)(GLenum, GLenum);
+GLboolean (APIENTRY *glUnmapBuffer)(GLenum);
+
+/* uniform */
+GLint (APIENTRY *glGetUniformLocation)(GLuint, const GLchar*);
+void (APIENTRY *glUniform1i)(GLint, GLint);
+void (APIENTRY *glUniform2f)(GLint, GLfloat, GLfloat);
+void (APIENTRY *glUniform3f)(GLint, GLfloat, GLfloat, GLfloat);
+void (APIENTRY *glUniform1iv)(GLint, GLsizei, const GLint*);
+
+/* texture */
+void (APIENTRY *glActiveTexture)(GLenum);
+
+/* program/shader */
+GLuint (APIENTRY *glCreateProgram)(void);
+void (APIENTRY *glLinkProgram)(GLuint);
+void (APIENTRY *glGetProgramiv)(GLuint, GLenum, GLint*);
+void (APIENTRY *glGetProgramInfoLog)(GLuint, GLsizei, GLsizei*, GLchar*);
+void (APIENTRY *glUseProgram)(GLuint);
+void (APIENTRY *glDeleteProgram)(GLuint);
+
+GLuint (APIENTRY *glCreateShader)(GLenum);
+void (APIENTRY *glShaderSource)(GLuint, GLsizei, const GLchar**, const GLint*);
+void (APIENTRY *glCompileShader)(GLuint);
+void (APIENTRY *glGetShaderiv)(GLuint, GLenum, GLint*);
+void (APIENTRY *glGetShaderInfoLog)(GLuint, GLsizei, GLsizei*, GLchar*);
+
+void (APIENTRY *glAttachShader)(GLuint, GLuint);
+void (APIENTRY *glDetachShader)(GLuint, GLuint);
+void (APIENTRY *glDeleteShader)(GLuint);
+
+//GL_DeleteBuffers glDeleteBuffers = NULL;
+
+void win32_gl_init() {
+ glGenBuffers = (void(APIENTRY*)(GLsizei, GLuint*))SDL_GL_GetProcAddress("glGenBuffers");
+ glDeleteBuffers = (void(APIENTRY*)(GLsizei, GLuint*))SDL_GL_GetProcAddress("glDeleteBuffers");
+ glBindBuffer = (void(APIENTRY*)(GLenum, GLuint))SDL_GL_GetProcAddress("glBindBuffer");
+ glBufferData = (void(APIENTRY*)(GLenum, GLsizeiptr, const GLvoid*, GLenum))SDL_GL_GetProcAddress("glBufferData");
+ glMapBuffer = (GLvoid*(APIENTRY*)(GLenum, GLenum))SDL_GL_GetProcAddress("glMapBuffer");
+ glUnmapBuffer = (GLboolean(APIENTRY*)(GLenum))SDL_GL_GetProcAddress("glUnmapBuffer");
+
+ glGetUniformLocation = (GLint(APIENTRY*)(GLuint, const GLchar*))SDL_GL_GetProcAddress("glGetUniformLocation");
+ glUniform1i = (void(APIENTRY*)(GLint, GLint))SDL_GL_GetProcAddress("glUniform1i");
+ glUniform2f = (void(APIENTRY*)(GLint, GLfloat, GLfloat))SDL_GL_GetProcAddress("glUniform2f");
+ glUniform3f = (void(APIENTRY*)(GLint, GLfloat, GLfloat, GLfloat))SDL_GL_GetProcAddress("glUniform3f");
+ glUniform1iv = (void(APIENTRY*)(GLint, GLsizei, const GLint*))SDL_GL_GetProcAddress("glUniform1iv");
+
+ glActiveTexture = (void(APIENTRY*)(GLenum))SDL_GL_GetProcAddress("glActiveTexture");
+
+ glCreateProgram = (GLuint(APIENTRY*)(void))SDL_GL_GetProcAddress("glCreateProgram");
+ glLinkProgram = (void(APIENTRY*)(GLuint))SDL_GL_GetProcAddress("glLinkProgram");
+ glGetProgramiv = (void(APIENTRY*)(GLuint, GLenum, GLint*))SDL_GL_GetProcAddress("glGetProgramiv");
+ glGetProgramInfoLog = (void(APIENTRY*)(GLuint, GLsizei, GLsizei*, GLchar*))SDL_GL_GetProcAddress("glGetProgramInfoLog");
+ glUseProgram = (void(APIENTRY*)(GLuint))SDL_GL_GetProcAddress("glUseProgram");
+ glDeleteProgram = (void(APIENTRY*)(GLuint))SDL_GL_GetProcAddress("glDeleteProgram");
+
+ glCreateShader = (GLuint(APIENTRY*)(GLenum))SDL_GL_GetProcAddress("glCreateShader");
+ glShaderSource = (void(APIENTRY*)(GLuint, GLsizei, const GLchar**, const GLint*))SDL_GL_GetProcAddress("glShaderSource");
+ glCompileShader = (void(APIENTRY*)(GLuint))SDL_GL_GetProcAddress("glCompileShader");
+ glGetShaderiv = (void(APIENTRY*)(GLuint, GLenum, GLint*))SDL_GL_GetProcAddress("glGetShaderiv");
+ glGetShaderInfoLog = (void(APIENTRY*)(GLuint, GLsizei, GLsizei*, GLchar*))SDL_GL_GetProcAddress("glGetShaderInfoLog");
+
+ glAttachShader = (void(APIENTRY*)(GLuint, GLuint))SDL_GL_GetProcAddress("glAttachShader");
+ glDetachShader = (void(APIENTRY*)(GLuint, GLuint))SDL_GL_GetProcAddress("glDetachShader");
+ glDeleteShader = (void(APIENTRY*)(GLuint))SDL_GL_GetProcAddress("glDeleteShader");
+}
+
+#endif
diff --git a/gl.h b/gl.h
index bb28c20..1bbd9a5 100644
--- a/gl.h
+++ b/gl.h
@@ -1,7 +1,51 @@
#ifndef GL_H
#define GL_H
+#ifndef WIN32
#define GL_GLEXT_PROTOTYPES
+#endif
+
#include <SDL_opengl.h>
+#ifdef WIN32
+/* buffer */
+extern void (APIENTRY *glGenBuffers)(GLsizei, GLuint*);
+extern void (APIENTRY *glDeleteBuffers)(GLsizei, GLuint*);
+extern void (APIENTRY *glBindBuffer)(GLenum, GLuint);
+extern void (APIENTRY *glBufferData)(GLenum, GLsizeiptr, const GLvoid*, GLenum);
+extern GLvoid* (APIENTRY *glMapBuffer)(GLenum, GLenum);
+extern GLboolean (APIENTRY *glUnmapBuffer)(GLenum);
+
+/* uniform */
+extern GLint (APIENTRY *glGetUniformLocation)(GLuint, const GLchar*);
+extern void (APIENTRY *glUniform1i)(GLint, GLint);
+extern void (APIENTRY *glUniform2f)(GLint, GLfloat, GLfloat);
+extern void (APIENTRY *glUniform3f)(GLint, GLfloat, GLfloat, GLfloat);
+extern void (APIENTRY *glUniform1iv)(GLint, GLsizei, const GLint*);
+
+/* texture */
+extern void (APIENTRY *glActiveTexture)(GLenum);
+
+/* program/shader */
+extern GLuint (APIENTRY *glCreateProgram)(void);
+extern void (APIENTRY *glLinkProgram)(GLuint);
+extern void (APIENTRY *glGetProgramiv)(GLuint, GLenum, GLint*);
+extern void (APIENTRY *glGetProgramInfoLog)(GLuint, GLsizei, GLsizei*, GLchar*);
+extern void (APIENTRY *glUseProgram)(GLuint);
+extern void (APIENTRY *glDeleteProgram)(GLuint);
+
+extern GLuint (APIENTRY *glCreateShader)(GLenum);
+extern void (APIENTRY *glShaderSource)(GLuint, GLsizei, const GLchar**, const GLint*);
+extern void (APIENTRY *glCompileShader)(GLuint);
+extern void (APIENTRY *glGetShaderiv)(GLuint, GLenum, GLint*);
+extern void (APIENTRY *glGetShaderInfoLog)(GLuint, GLsizei, GLsizei*, GLchar*);
+
+extern void (APIENTRY *glAttachShader)(GLuint, GLuint);
+extern void (APIENTRY *glDetachShader)(GLuint, GLuint);
+extern void (APIENTRY *glDeleteShader)(GLuint);
+
+void win32_gl_init();
+#endif
+
+
#endif
diff --git a/main.cpp b/main.cpp
index d6ad962..4d050fc 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,25 +3,44 @@
#include "gl.h"
+#include <SDL_image.h>
+
+bool init_sdl_image() {
+ int real_flags = IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
+ if(!(real_flags | IMG_INIT_PNG && real_flags | IMG_INIT_JPG)) {
+ std::cerr << "Missing PNG or JPEG libraries" << std::endl;
+ return false;
+ }
+ return true;
+}
+
int main(int argc, char **argv) {
+ if(!init_sdl_image())
+ return 1;
+
video::width = 1280;
video::height = 720;
video::init();
- Scene scene;
+#ifdef WIN32
+ win32_gl_init();
+#endif
+
+ Scene *scene = new Scene();
SDL_ShowCursor(SDL_DISABLE);
SDL_WarpMouse(video::width/2, video::height/2);
- scene.last_time = SDL_GetTicks();
- scene.update();
- while(scene.running) {
- scene.events();
- scene.render();
+ scene->last_time = SDL_GetTicks();
+ scene->update();
+ while(scene->running) {
+ scene->events();
+ scene->render();
SDL_Delay(1);
}
+ delete scene;
video::free();
return 0;
diff --git a/scene.cpp b/scene.cpp
index bf49c57..e371f74 100644
--- a/scene.cpp
+++ b/scene.cpp
@@ -7,6 +7,7 @@
#include "gl.h"
+#include <unistd.h>
#include <cmath>
#include <queue>
@@ -34,10 +35,9 @@ Scene::Scene() {
terrain_program.link();
terrain_program.use();
- GLint tex1loc = glGetUniformLocation(terrain_program.get_program(), "tex[1]");
- glUniform1i(tex1loc, 1);
- GLint tex2loc = glGetUniformLocation(terrain_program.get_program(), "tex[2]");
- glUniform1i(tex2loc, 2);
+ GLint tex = glGetUniformLocation(terrain_program.get_program(), "tex");
+ GLint texv[] = {0, 1, 2};
+ glUniform1iv(tex, 3, texv);
GLint markloc = glGetUniformLocation(terrain_program.get_program(), "marktex");
glUniform1i(markloc, 3);
glUseProgram(0);
@@ -394,14 +394,8 @@ void Scene::render() {
Terrain::Chunk *chunk = *it;
glPushMatrix();
glTranslatef(-pos.x, -pos.y, -pos.z);
- /*if(!chunk->nodes) {
- for(int i = 0; i < 4; i++)
- q.push(chunk->children[i]);
- continue;
- } else*/ if(chunk->vbo_object) {
- for(unsigned int i = 0; i < chunk->node_count; i++)
- chunk->nodes[i]->draw_grid();
- }
+ for(unsigned int i = 0; i < chunk->node_count; i++)
+ chunk->nodes[i]->draw_grid();
glPopMatrix();
}
}
diff --git a/scene.h b/scene.h
index 82c1100..72bf506 100644
--- a/scene.h
+++ b/scene.h
@@ -36,6 +36,8 @@ class Scene {
int sx, sy;
GLShaderProgram terrain_program;
+ //GLVertexShader terrain_vertex;
+ //GLFragmentShader terrain_fragment;
GLuint grass_texture, rock_texture, soil_texture, marker_texture;
diff --git a/terrain.cpp b/terrain.cpp
index 35aef58..1fd75db 100644
--- a/terrain.cpp
+++ b/terrain.cpp
@@ -73,6 +73,19 @@ void Terrain::Node::fill() {
vertex_array[11] = y;
}
+void Terrain::Node::draw() {
+ glBegin(GL_TRIANGLES);
+ for(int i = 0; i < 2; i++) {
+ glTexCoord2f(0, 0);
+ glVertex3f(vertex_array[0], vertex_array[1], vertex_array[2]);
+ glTexCoord2f(i, 1);
+ glVertex3f(vertex_array[i*3+3], vertex_array[i*3+4], vertex_array[i*3+5]);
+ glTexCoord2f(1, 1-i);
+ glVertex3f(vertex_array[i*3+6], vertex_array[i*3+7], vertex_array[i*3+8]);
+ }
+ glEnd();
+}
+
void Terrain::Node::draw_grid() {
glNormal3f(0, 1, 0);
glBegin(GL_LINE_LOOP);
diff --git a/terrain.h b/terrain.h
index 736b719..84abe26 100644
--- a/terrain.h
+++ b/terrain.h
@@ -23,6 +23,7 @@ class Terrain {
float distance(float px, float pz);
void fill();
+ void draw();
void draw_grid();
void draw_normal();
float get_height(float px, float py);