Initial commit
This commit is contained in:
commit
0543ad9a93
4
.gitignore
vendored
Executable file
4
.gitignore
vendored
Executable file
@ -0,0 +1,4 @@
|
||||
build
|
||||
*~
|
||||
KubKraft
|
||||
|
22
KubKraft.vimproj
Executable file
22
KubKraft.vimproj
Executable file
@ -0,0 +1,22 @@
|
||||
KubKraft=/home/quentin/Bureau/KubKraft CD=. filter=".gitignore *.c *.h *.diff *.vim AUTHORS ChangeLog COPYING Makefile Notes TODO *.textile *.cpp" {
|
||||
Makefile
|
||||
include=include {
|
||||
biome.h
|
||||
config.h
|
||||
cube.h
|
||||
init.h
|
||||
map.h
|
||||
player.h
|
||||
scene.h
|
||||
types.h
|
||||
}
|
||||
source=source {
|
||||
biome.cpp
|
||||
cube.cpp
|
||||
init.cpp
|
||||
main.cpp
|
||||
map.cpp
|
||||
player.cpp
|
||||
scene.cpp
|
||||
}
|
||||
}
|
114
Makefile
Executable file
114
Makefile
Executable file
@ -0,0 +1,114 @@
|
||||
#---------------------------------------------------------------------------------
|
||||
# Compiler executables
|
||||
#---------------------------------------------------------------------------------
|
||||
CC := gcc
|
||||
CXX := g++
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# Options for code generation
|
||||
#---------------------------------------------------------------------------------
|
||||
CFLAGS := -g -Wall
|
||||
CXXFLAGS:= $(CFLAGS) --std=c++0x
|
||||
LDFLAGS := -g -Wl
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# Any extra libraries you wish to link with your project
|
||||
#---------------------------------------------------------------------------------
|
||||
LIBS := -lGL -lGLU -lSDL
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# list of directories containing libraries, this must be the top level containing
|
||||
# include and lib
|
||||
#---------------------------------------------------------------------------------
|
||||
LIBDIRS :=
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# Source folders and executable name
|
||||
#---------------------------------------------------------------------------------
|
||||
TARGET := $(shell basename $(CURDIR))
|
||||
BUILD := build
|
||||
SOURCES := source
|
||||
INCLUDES:= source include
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# Source files
|
||||
#---------------------------------------------------------------------------------
|
||||
ifneq ($(BUILD),$(notdir $(CURDIR)))
|
||||
#---------------------------------------------------------------------------------
|
||||
export OUTPUT := $(CURDIR)/$(TARGET)
|
||||
|
||||
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
|
||||
|
||||
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
|
||||
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# Use CXX for linking C++ projects, CC for standard C
|
||||
#---------------------------------------------------------------------------------
|
||||
ifeq ($(strip $(CPPFILES)),)
|
||||
#---------------------------------------------------------------------------------
|
||||
export LD := $(CC)
|
||||
#---------------------------------------------------------------------------------
|
||||
else
|
||||
#---------------------------------------------------------------------------------
|
||||
export LD := $(CXX)
|
||||
#---------------------------------------------------------------------------------
|
||||
endif
|
||||
#---------------------------------------------------------------------------------
|
||||
|
||||
export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o)
|
||||
|
||||
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir))
|
||||
|
||||
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
.PHONY: $(BUILD) clean install uninstall
|
||||
#------------------------------------------------------------------------------
|
||||
$(BUILD):
|
||||
@[ -d $@ ] || mkdir -p $@
|
||||
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
clean:
|
||||
@echo clean ...
|
||||
@rm -fr $(BUILD) $(TARGET)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
install:
|
||||
@cp -u $(TARGET) /usr/bin/$(TARGET)
|
||||
@echo installed.
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
uninstall:
|
||||
@rm -f /usr/bin/$(TARGET)
|
||||
@echo uninstalled.
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
else
|
||||
#---------------------------------------------------------------------------------
|
||||
# Makefile targets
|
||||
#---------------------------------------------------------------------------------
|
||||
all: $(OUTPUT)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
$(OUTPUT): $(OFILES)
|
||||
@echo built ... $(notdir $@)
|
||||
@$(LD) $(LDFLAGS) $(OFILES) $(LIBS) -o $@
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
%.o: %.c
|
||||
#---------------------------------------------------------------------------------
|
||||
@echo $(notdir $<)
|
||||
@$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
%.o: %.cpp
|
||||
#---------------------------------------------------------------------------------
|
||||
@echo $(notdir $<)
|
||||
@$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
endif
|
||||
#---------------------------------------------------------------------------------
|
||||
|
BIN
include/.cube.h.swp
Normal file
BIN
include/.cube.h.swp
Normal file
Binary file not shown.
19
include/biome.h
Normal file
19
include/biome.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef BIOME_H
|
||||
#define BIOME_H
|
||||
|
||||
class Biome {
|
||||
public:
|
||||
Biome(float x, float y, float z, GLuint texture);
|
||||
~Biome();
|
||||
|
||||
void draw();
|
||||
|
||||
private:
|
||||
float m_x;
|
||||
float m_y;
|
||||
float m_z;
|
||||
|
||||
std::vector<Cube*> m_cubes;
|
||||
};
|
||||
|
||||
#endif // BIOME_H
|
14
include/config.h
Normal file
14
include/config.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#define WIN_WIDTH 640
|
||||
#define WIN_HEIGHT 480
|
||||
|
||||
#define APP_LABEL "Test OpenGL app"
|
||||
|
||||
#define NEAR 0.1
|
||||
#define FAR 1000.0
|
||||
|
||||
#define VISION_ANGLE 70.0
|
||||
|
||||
#endif // CONFIG_H
|
21
include/cube.h
Normal file
21
include/cube.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef CUBE_H
|
||||
#define CUBE_H
|
||||
|
||||
class Cube {
|
||||
public:
|
||||
Cube(float x, float y, float z, GLuint texture, Map *map);
|
||||
~Cube() {}
|
||||
|
||||
void draw(float x, float y, float z);
|
||||
|
||||
private:
|
||||
float m_x;
|
||||
float m_y;
|
||||
float m_z;
|
||||
|
||||
GLuint m_texture;
|
||||
|
||||
Map *m_map;
|
||||
};
|
||||
|
||||
#endif // CUBE_H
|
7
include/init.h
Normal file
7
include/init.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef INIT_H
|
||||
#define INIT_H
|
||||
|
||||
void initSDL();
|
||||
void initOpenGL();
|
||||
|
||||
#endif // INIT_H
|
11
include/map.h
Normal file
11
include/map.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef MAP_H
|
||||
#define MAP_H
|
||||
|
||||
typedef struct {
|
||||
u16 width;
|
||||
u16 depth;
|
||||
u16 height;
|
||||
u16 *map;
|
||||
} Map;
|
||||
|
||||
#endif // MAP_H
|
27
include/player.h
Normal file
27
include/player.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player(float x, float y, float z, float angle);
|
||||
|
||||
void move(float distance, float direction);
|
||||
void turnH(float angle);
|
||||
void turnV(float angle);
|
||||
void fly();
|
||||
void land();
|
||||
|
||||
void watch();
|
||||
|
||||
private:
|
||||
float m_x;
|
||||
float m_y;
|
||||
float m_z;
|
||||
|
||||
float m_angleH;
|
||||
float m_angleV;
|
||||
|
||||
float m_eyeheight;
|
||||
};
|
||||
|
||||
#endif // PLAYER_H
|
33
include/scene.h
Normal file
33
include/scene.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene();
|
||||
~Scene();
|
||||
|
||||
void exec();
|
||||
|
||||
void manageEvents();
|
||||
void animate();
|
||||
void draw();
|
||||
void display();
|
||||
|
||||
void loadTextures();
|
||||
void drawField();
|
||||
|
||||
void lockMouse();
|
||||
void unlockMouse();
|
||||
|
||||
private:
|
||||
typedef std::map<std::string, GLuint> Textures;
|
||||
|
||||
bool m_cont;
|
||||
Textures m_textures;
|
||||
|
||||
Player *m_player;
|
||||
|
||||
Biome *m_biome;
|
||||
};
|
||||
|
||||
#endif // SCENE_H
|
18
include/sdlglutils.h
Normal file
18
include/sdlglutils.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef SDLGLUTILS_H
|
||||
#define SDLGLUTILS_H
|
||||
|
||||
#include <GL/gl.h>
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
#ifndef GL_CLAMP_TO_EDGE
|
||||
#define GL_CLAMP_TO_EDGE 0x812F
|
||||
#endif
|
||||
|
||||
GLuint loadTexture(const char * filename,bool useMipMap = true);
|
||||
int takeScreenshot(const char * filename);
|
||||
void drawAxis(double scale = 1);
|
||||
int initFullScreen(unsigned int * width = NULL,unsigned int * height = NULL);
|
||||
int XPMFromImage(const char * imagefile, const char * XPMfile);
|
||||
SDL_Cursor * cursorFromXPM(const char * xpm[]);
|
||||
|
||||
#endif //SDLGLUTILS_H
|
11
include/types.h
Normal file
11
include/types.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef TYPES_H
|
||||
#define TYPES_H
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef signed char s8;
|
||||
typedef unsigned short u16;
|
||||
typedef signed short s16;
|
||||
typedef unsigned long u32;
|
||||
typedef signed long s32;
|
||||
|
||||
#endif // TYPES_H
|
BIN
source/.scene.cpp.swp
Normal file
BIN
source/.scene.cpp.swp
Normal file
Binary file not shown.
62
source/biome.cpp
Normal file
62
source/biome.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "map.h"
|
||||
#include "cube.h"
|
||||
#include "biome.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
u16 width = 3;
|
||||
u16 depth = 3;
|
||||
u16 height = 2;
|
||||
|
||||
u16 map[3 * 3 * 2] = {
|
||||
1, 1, 1,
|
||||
1, 1, 1,
|
||||
1, 1, 1,
|
||||
|
||||
0, 0, 0,
|
||||
0, 1, 0,
|
||||
0, 0, 0
|
||||
};
|
||||
|
||||
Map *m_map = new Map{width, depth, height, map};
|
||||
|
||||
Biome::Biome(float x, float y, float z, GLuint texture) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
|
||||
for(u16 z = 0 ; z < 2 ; z++) {
|
||||
for(u16 y = 0 ; y < 3 ; y++) {
|
||||
for(u16 x = 0 ; x < 3 ; x++) {
|
||||
if(map[x + (y * m_map->depth) + (z * m_map->width * m_map->depth)] == 1) {
|
||||
m_cubes.push_back(new Cube(x, y, z, texture, m_map));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Biome::~Biome() {
|
||||
for(std::vector<Cube*>::iterator it = m_cubes.begin() ; it != m_cubes.end() ; it++) {
|
||||
delete (*it);
|
||||
}
|
||||
|
||||
m_cubes.clear();
|
||||
|
||||
delete m_map;
|
||||
}
|
||||
|
||||
void Biome::draw() {
|
||||
for(std::vector<Cube*>::iterator it = m_cubes.begin() ; it != m_cubes.end() ; it++) {
|
||||
(*it)->draw(m_x, m_y, m_z);
|
||||
}
|
||||
}
|
68
source/cube.cpp
Normal file
68
source/cube.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
#include <string>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "map.h"
|
||||
#include "cube.h"
|
||||
|
||||
Cube::Cube(float x, float y, float z, GLuint texture, Map *map) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
m_texture = texture;
|
||||
|
||||
m_map = map;
|
||||
}
|
||||
|
||||
void Cube::draw(float x, float y, float z) {
|
||||
m_x += x;
|
||||
m_y += y;
|
||||
m_z += z;
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, m_texture);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
glColor3ub(255, 255, 255); // Front Right
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x + 1, m_y + 1, m_z + 1);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x + 1, m_y + 1, m_z);
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x, m_y + 1, m_z);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x, m_y + 1, m_z + 1);
|
||||
|
||||
glColor3ub(255, 255, 255); // Front left
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x + 1, m_y, m_z + 1);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x + 1, m_y, m_z);
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x + 1, m_y + 1, m_z);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x + 1, m_y + 1, m_z + 1);
|
||||
|
||||
glColor3ub(255, 255, 255); // Back left
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x, m_y, m_z + 1);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x, m_y, m_z);
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x + 1, m_y, m_z);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x + 1, m_y, m_z + 1);
|
||||
|
||||
glColor3ub(255, 255, 255); // Back right
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x, m_y + 1, m_z + 1);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x, m_y + 1, m_z);
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x, m_y, m_z);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x, m_y, m_z + 1);
|
||||
|
||||
glColor3ub(255, 255, 255); // Bottom
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x + 1, m_y + 1, m_z);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x + 1, m_y, m_z);
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x, m_y, m_z);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x, m_y, m_z);
|
||||
|
||||
glColor3ub(255, 255, 255); // Top
|
||||
glTexCoord2d(0, 1); glVertex3d(m_x, m_y, m_z + 1);
|
||||
glTexCoord2d(0, 0); glVertex3d(m_x + 1, m_y, m_z + 1);
|
||||
glTexCoord2d(1, 0); glVertex3d(m_x + 1, m_y + 1, m_z + 1);
|
||||
glTexCoord2d(1, 1); glVertex3d(m_x, m_y + 1, m_z + 1);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
39
source/init.cpp
Normal file
39
source/init.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "init.h"
|
||||
#include "config.h"
|
||||
|
||||
void initSDL() {
|
||||
// Start SDL with video module
|
||||
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Error while initializing SDL: %s\n", SDL_GetError());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Creation of the window initialized to work with OpenGL
|
||||
SDL_SetVideoMode(WIN_WIDTH, WIN_HEIGHT, 32, SDL_OPENGL);
|
||||
|
||||
// Label of application
|
||||
SDL_WM_SetCaption(APP_LABEL, NULL);
|
||||
}
|
||||
|
||||
void initOpenGL() {
|
||||
glEnable(GL_CULL_FACE); // Turn on hidden face tagging
|
||||
glCullFace(GL_BACK); // Hidden faces = back faces
|
||||
glFrontFace(GL_CCW); // Front face = Trigo way
|
||||
|
||||
// Clean screen RGBa color
|
||||
glClearColor(0.5, 0.5, 1.0, 0);
|
||||
|
||||
// Window definition
|
||||
glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
// Visible area definition
|
||||
gluPerspective(VISION_ANGLE, (GLdouble)WIN_WIDTH / (GLdouble)WIN_HEIGHT, NEAR, FAR);
|
||||
}
|
||||
|
33
source/main.cpp
Executable file
33
source/main.cpp
Executable file
@ -0,0 +1,33 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "sdlglutils.h"
|
||||
|
||||
#include "types.h"
|
||||
#include "init.h"
|
||||
#include "map.h"
|
||||
#include "cube.h"
|
||||
#include "biome.h"
|
||||
#include "player.h"
|
||||
#include "scene.h"
|
||||
|
||||
int main() {
|
||||
// Init SDL and OpenGL
|
||||
initSDL();
|
||||
initOpenGL();
|
||||
|
||||
// Scene execution
|
||||
Scene scene;
|
||||
scene.exec();
|
||||
|
||||
// Stop SDL
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
0
source/map.cpp
Normal file
0
source/map.cpp
Normal file
72
source/player.cpp
Normal file
72
source/player.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "player.h"
|
||||
|
||||
Player::Player(float x, float y, float z, float angle) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
m_angleH = angle;
|
||||
m_angleV = 0.0f;
|
||||
m_eyeheight = 0.8;
|
||||
}
|
||||
|
||||
void Player::move(float distance, float direction) {
|
||||
direction += m_angleH;
|
||||
|
||||
m_y -= distance * sin(direction * M_PI / 180.0);
|
||||
m_x -= distance * cos(direction * M_PI / 180.0);
|
||||
}
|
||||
|
||||
void Player::turnH(float angle) {
|
||||
m_angleH += angle;
|
||||
|
||||
while(m_angleH >= 180.0f) {
|
||||
m_angleH -= 360.0f;
|
||||
}
|
||||
while(m_angleH < -180.0f) {
|
||||
m_angleH += 360.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::turnV(float angle) {
|
||||
m_angleV += angle;
|
||||
|
||||
if(45.0f < m_angleV) {
|
||||
m_angleV = 45.0f;
|
||||
}
|
||||
else if(-45.0f > m_angleV) {
|
||||
m_angleV = -45.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::fly() {
|
||||
if(m_eyeheight < 20) m_eyeheight += 0.05;
|
||||
}
|
||||
|
||||
void Player::land() {
|
||||
if(m_eyeheight > 0.8) m_eyeheight -= 0.05;
|
||||
}
|
||||
|
||||
void Player::watch() {
|
||||
#define RADIANS_PER_DEGREES 0.0174532925199
|
||||
|
||||
gluLookAt(
|
||||
// Eye position
|
||||
m_x, m_y, m_eyeheight,
|
||||
|
||||
// Point targeted
|
||||
m_x - cos(-m_angleH * RADIANS_PER_DEGREES),
|
||||
m_y + sin(-m_angleH * RADIANS_PER_DEGREES),
|
||||
m_eyeheight + tan(m_angleV * RADIANS_PER_DEGREES),
|
||||
|
||||
// z is the vertical
|
||||
0, 0, 1);
|
||||
}
|
||||
|
197
source/scene.cpp
Normal file
197
source/scene.cpp
Normal file
@ -0,0 +1,197 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "sdlglutils.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "types.h"
|
||||
#include "player.h"
|
||||
#include "map.h"
|
||||
#include "cube.h"
|
||||
#include "biome.h"
|
||||
#include "scene.h"
|
||||
|
||||
Scene::Scene() {
|
||||
m_player = new Player(2, 2, 0, -130);
|
||||
|
||||
loadTextures();
|
||||
|
||||
m_biome = new Biome(0, 0, 0, m_textures["brick"]);
|
||||
}
|
||||
|
||||
Scene::~Scene() {
|
||||
// Deleting loaded textures
|
||||
for(Textures::iterator element = m_textures.begin() ; element != m_textures.end() ; element++) {
|
||||
glDeleteTextures(1, &element->second);
|
||||
element->second = 0;
|
||||
}
|
||||
|
||||
delete m_biome;
|
||||
delete m_player;
|
||||
}
|
||||
|
||||
void Scene::exec() {
|
||||
lockMouse();
|
||||
|
||||
m_cont = true;
|
||||
|
||||
while(m_cont) {
|
||||
manageEvents();
|
||||
animate();
|
||||
draw();
|
||||
display();
|
||||
}
|
||||
|
||||
unlockMouse();
|
||||
}
|
||||
|
||||
void Scene::manageEvents() {
|
||||
SDL_Event event;
|
||||
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
m_cont = false;
|
||||
break;
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
if((WIN_WIDTH / 2) != event.motion.x || (WIN_HEIGHT / 2) != event.motion.y) {
|
||||
m_player->turnH(-event.motion.xrel * 0.06);
|
||||
m_player->turnV(-event.motion.yrel * 0.06);
|
||||
|
||||
SDL_WarpMouse((WIN_WIDTH / 2), (WIN_HEIGHT / 2));
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
if(event.key.keysym.sym == SDLK_ESCAPE) {
|
||||
m_cont = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::animate() {
|
||||
// Read keys state
|
||||
u8 *keys = SDL_GetKeyState(NULL);
|
||||
|
||||
float direction = 0.0;
|
||||
bool movement = false;
|
||||
|
||||
// Forward
|
||||
if(keys[SDLK_z]) {
|
||||
movement = true;
|
||||
|
||||
if(keys[SDLK_q]) direction = 45.0;
|
||||
else if(keys[SDLK_d]) direction = -45.0;
|
||||
|
||||
else direction = 0.0;
|
||||
}
|
||||
|
||||
// Back
|
||||
else if(keys[SDLK_s]) {
|
||||
movement = true;
|
||||
|
||||
if(keys[SDLK_q]) direction = 135.0;
|
||||
else if(keys[SDLK_d]) direction = -135.0;
|
||||
|
||||
else direction = 180.0;
|
||||
}
|
||||
|
||||
if(movement == false) {
|
||||
// Left
|
||||
if(keys[SDLK_q]) {
|
||||
direction = 90.0;
|
||||
movement = true;
|
||||
}
|
||||
|
||||
// Right
|
||||
else if(keys[SDLK_d]) {
|
||||
direction = -90.0;
|
||||
movement = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(keys[SDLK_SPACE]) {
|
||||
m_player->fly();
|
||||
}
|
||||
else if(keys[SDLK_LSHIFT]) {
|
||||
m_player->land();
|
||||
}
|
||||
|
||||
if(movement) {
|
||||
#define MOVEMENT_SPEED (2.0f)
|
||||
|
||||
float distance = 20 * MOVEMENT_SPEED / 1000.0f;
|
||||
|
||||
m_player->move(distance, direction);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::draw() {
|
||||
// Clean screen
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Put camera
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
//gluLookAt(3, 4, 2, 0, 0, 0, 0, 0, 1);
|
||||
m_player->watch();
|
||||
|
||||
// Drawing field
|
||||
drawField();
|
||||
}
|
||||
|
||||
void Scene::display() {
|
||||
glFlush();
|
||||
|
||||
SDL_GL_SwapBuffers();
|
||||
}
|
||||
|
||||
void Scene::loadTextures() {
|
||||
// Load ground textures
|
||||
m_textures["ground"] = loadTexture("textures/ground.bmp");
|
||||
m_textures["brick"] = loadTexture("textures/brick.bmp");
|
||||
}
|
||||
|
||||
void Scene::drawField() {
|
||||
// Turn on textures
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
// Select texture
|
||||
glBindTexture(GL_TEXTURE_2D, m_textures["ground"]);
|
||||
|
||||
// Add texture to scene
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
glColor3ub(255, 255, 255);
|
||||
glTexCoord2d(0, 1); glVertex3d(0, 0, 0);
|
||||
glTexCoord2d(0, 0); glVertex3d(50, 0, 0);
|
||||
glTexCoord2d(1, 0); glVertex3d(50, 50, 0);
|
||||
glTexCoord2d(1, 1); glVertex3d(0, 50, 0);
|
||||
|
||||
glEnd();
|
||||
|
||||
m_biome->draw();
|
||||
}
|
||||
|
||||
void Scene::lockMouse() {
|
||||
SDL_WM_GrabInput(SDL_GRAB_ON);
|
||||
|
||||
SDL_WarpMouse((WIN_WIDTH / 2), (WIN_HEIGHT / 2));
|
||||
|
||||
SDL_ShowCursor(false);
|
||||
}
|
||||
|
||||
void Scene::unlockMouse() {
|
||||
SDL_WM_GrabInput(SDL_GRAB_OFF);
|
||||
|
||||
SDL_ShowCursor(true);
|
||||
}
|
||||
|
329
source/sdlglutils.cpp
Normal file
329
source/sdlglutils.cpp
Normal file
@ -0,0 +1,329 @@
|
||||
#include "sdlglutils.h"
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
SDL_Surface * flipSurface(SDL_Surface * surface);
|
||||
|
||||
GLuint loadTexture(const char * filename,bool useMipMap)
|
||||
{
|
||||
GLuint glID;
|
||||
SDL_Surface * picture_surface = NULL;
|
||||
SDL_Surface *gl_surface = NULL;
|
||||
SDL_Surface * gl_fliped_surface = NULL;
|
||||
Uint32 rmask, gmask, bmask, amask;
|
||||
|
||||
picture_surface = SDL_LoadBMP(filename);
|
||||
if (picture_surface == NULL)
|
||||
return 0;
|
||||
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
|
||||
rmask = 0xff000000;
|
||||
gmask = 0x00ff0000;
|
||||
bmask = 0x0000ff00;
|
||||
amask = 0x000000ff;
|
||||
#else
|
||||
|
||||
rmask = 0x000000ff;
|
||||
gmask = 0x0000ff00;
|
||||
bmask = 0x00ff0000;
|
||||
amask = 0xff000000;
|
||||
#endif
|
||||
|
||||
SDL_PixelFormat format = *(picture_surface->format);
|
||||
format.BitsPerPixel = 32;
|
||||
format.BytesPerPixel = 4;
|
||||
format.Rmask = rmask;
|
||||
format.Gmask = gmask;
|
||||
format.Bmask = bmask;
|
||||
format.Amask = amask;
|
||||
|
||||
gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE);
|
||||
|
||||
gl_fliped_surface = flipSurface(gl_surface);
|
||||
|
||||
glGenTextures(1, &glID);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, glID);
|
||||
|
||||
|
||||
if (useMipMap)
|
||||
{
|
||||
|
||||
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w,
|
||||
gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE,
|
||||
gl_fliped_surface->pixels);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_LINEAR);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
|
||||
gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
|
||||
gl_fliped_surface->pixels);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
}
|
||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
|
||||
|
||||
|
||||
SDL_FreeSurface(gl_fliped_surface);
|
||||
SDL_FreeSurface(gl_surface);
|
||||
SDL_FreeSurface(picture_surface);
|
||||
|
||||
return glID;
|
||||
}
|
||||
|
||||
int takeScreenshot(const char * filename)
|
||||
{
|
||||
GLint viewport[4];
|
||||
Uint32 rmask, gmask, bmask, amask;
|
||||
SDL_Surface * picture, * finalpicture;
|
||||
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
|
||||
rmask = 0xff000000;
|
||||
gmask = 0x00ff0000;
|
||||
bmask = 0x0000ff00;
|
||||
amask = 0x000000ff;
|
||||
#else
|
||||
|
||||
rmask = 0x000000ff;
|
||||
gmask = 0x0000ff00;
|
||||
bmask = 0x00ff0000;
|
||||
amask = 0xff000000;
|
||||
#endif
|
||||
|
||||
picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32,
|
||||
rmask, gmask, bmask, amask);
|
||||
SDL_LockSurface(picture);
|
||||
glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA,
|
||||
GL_UNSIGNED_BYTE,picture->pixels);
|
||||
SDL_UnlockSurface(picture);
|
||||
|
||||
finalpicture = flipSurface(picture);
|
||||
|
||||
if (SDL_SaveBMP(finalpicture, filename))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
SDL_FreeSurface(finalpicture);
|
||||
SDL_FreeSurface(picture);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Surface * flipSurface(SDL_Surface * surface)
|
||||
{
|
||||
int current_line,pitch;
|
||||
SDL_Surface * fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
|
||||
surface->w,surface->h,
|
||||
surface->format->BitsPerPixel,
|
||||
surface->format->Rmask,
|
||||
surface->format->Gmask,
|
||||
surface->format->Bmask,
|
||||
surface->format->Amask);
|
||||
|
||||
|
||||
|
||||
SDL_LockSurface(surface);
|
||||
SDL_LockSurface(fliped_surface);
|
||||
|
||||
pitch = surface->pitch;
|
||||
for (current_line = 0; current_line < surface->h; current_line ++)
|
||||
{
|
||||
memcpy(&((unsigned char* )fliped_surface->pixels)[current_line*pitch],
|
||||
&((unsigned char* )surface->pixels)[(surface->h - 1 -
|
||||
current_line)*pitch],
|
||||
pitch);
|
||||
}
|
||||
|
||||
SDL_UnlockSurface(fliped_surface);
|
||||
SDL_UnlockSurface(surface);
|
||||
return fliped_surface;
|
||||
}
|
||||
|
||||
void drawAxis(double scale)
|
||||
{
|
||||
glPushAttrib(GL_ALL_ATTRIB_BITS);
|
||||
glPushMatrix();
|
||||
glDisable(GL_LIGHTING);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glLineWidth(2);
|
||||
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
|
||||
glScaled(scale,scale,scale);
|
||||
glBegin(GL_LINES);
|
||||
glColor3ub(255,0,0);
|
||||
glVertex3i(0,0,0);
|
||||
glVertex3i(1,0,0);
|
||||
glColor3ub(0,255,0);
|
||||
glVertex3i(0,0,0);
|
||||
glVertex3i(0,1,0);
|
||||
glColor3ub(0,0,255);
|
||||
glVertex3i(0,0,0);
|
||||
glVertex3i(0,0,1);
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
glPopAttrib();
|
||||
}
|
||||
|
||||
int initFullScreen(unsigned int * width,unsigned int * height)
|
||||
{
|
||||
SDL_Rect ** modes;
|
||||
|
||||
modes = SDL_ListModes(NULL,SDL_FULLSCREEN|SDL_OPENGL);
|
||||
if ((modes == (SDL_Rect **)0)||(modes == (SDL_Rect **)-1))
|
||||
return 0;
|
||||
|
||||
if (width != NULL)
|
||||
*width = modes[0]->w;
|
||||
if (height != NULL)
|
||||
*height = modes[0]->h;
|
||||
if (SDL_SetVideoMode(modes[0]->w,
|
||||
modes[0]->h,
|
||||
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
|
||||
SDL_FULLSCREEN|SDL_OPENGL) == NULL)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int XPMFromImage(const char * imagefile, const char * XPMfile)
|
||||
{
|
||||
SDL_Surface * image,*image32bits;
|
||||
FILE * xpm;
|
||||
Uint32 pixel;
|
||||
Uint8 r,g,b,a;
|
||||
int x,y;
|
||||
unsigned int w;
|
||||
char * xpm_name;
|
||||
Uint32 rmask, gmask, bmask, amask;
|
||||
|
||||
image = SDL_LoadBMP(imagefile);
|
||||
if (image == NULL)
|
||||
return -1;
|
||||
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
|
||||
rmask = 0xff000000;
|
||||
gmask = 0x00ff0000;
|
||||
bmask = 0x0000ff00;
|
||||
amask = 0x000000ff;
|
||||
#else
|
||||
|
||||
rmask = 0x000000ff;
|
||||
gmask = 0x0000ff00;
|
||||
bmask = 0x00ff0000;
|
||||
amask = 0xff000000;
|
||||
#endif
|
||||
|
||||
image32bits = SDL_CreateRGBSurface(SDL_SWSURFACE,
|
||||
image->w,image->h,
|
||||
32,rmask, gmask, bmask, amask);
|
||||
|
||||
SDL_BlitSurface(image,NULL,image32bits,NULL);
|
||||
SDL_FreeSurface(image);
|
||||
|
||||
xpm = fopen(XPMfile,"w");
|
||||
|
||||
xpm_name = (char*)malloc(strlen(imagefile)*sizeof(char));
|
||||
strcpy(xpm_name,imagefile);
|
||||
if (strchr(xpm_name,'.') != NULL)
|
||||
*strchr(xpm_name,'.') = '\0';
|
||||
fprintf(xpm,"const char *%s[] =\n",xpm_name);
|
||||
free(xpm_name);
|
||||
|
||||
fprintf(xpm,"\t{\n");
|
||||
fprintf(xpm,"\t\t/* width height num_colors chars_per_pixel */\n");
|
||||
w = ((image->w%8) == 0)?image32bits->w:8*(image32bits->w/8+1);
|
||||
|
||||
fprintf(xpm,"\t\t\" %d %d 3 1 \",\n",w,image32bits->h);
|
||||
fprintf(xpm,"\t\t/* colors */\n");
|
||||
fprintf(xpm,"\t\t\"X c #000000\",\n");
|
||||
fprintf(xpm,"\t\t\". c #ffffff\",\n");
|
||||
fprintf(xpm,"\t\t\" c None\",\n");
|
||||
fprintf(xpm,"\t\t/* pixels */\n");
|
||||
|
||||
SDL_LockSurface(image32bits);
|
||||
|
||||
for (y = 0; y < image32bits->h; y ++)
|
||||
{
|
||||
fprintf(xpm,"\t\t\"");
|
||||
for (x = 0; x < image32bits->w ; x ++)
|
||||
{
|
||||
pixel = ((Uint32*)image32bits->pixels)[x+y*image32bits->pitch/4];
|
||||
SDL_GetRGBA(pixel,image32bits->format,&r,&g,&b,&a);
|
||||
if (a < 128)
|
||||
fprintf(xpm," ");
|
||||
else if ((r >= 128)||(g >= 128)||(b >= 128))
|
||||
fprintf(xpm,".");
|
||||
else
|
||||
fprintf(xpm,"X");
|
||||
}
|
||||
for (x = image32bits->w ; (unsigned int)x < w ; x ++)
|
||||
fprintf(xpm," ");
|
||||
fprintf(xpm,"\",\n");
|
||||
}
|
||||
|
||||
SDL_UnlockSurface(image32bits);
|
||||
SDL_FreeSurface(image32bits);
|
||||
fprintf(xpm,"\t\t\"0,0\"\n");
|
||||
fprintf(xpm,"\t};\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Cursor * cursorFromXPM(const char * xpm[])
|
||||
{
|
||||
int i, row, col;
|
||||
int width, height;
|
||||
Uint8 * data;
|
||||
Uint8 * mask;
|
||||
int hot_x, hot_y;
|
||||
SDL_Cursor * cursor = NULL;
|
||||
|
||||
sscanf(xpm[0], "%d %d", &width, &height);
|
||||
data = (Uint8*)calloc(width/8*height,sizeof(Uint8));
|
||||
mask = (Uint8*)calloc(width/8*height,sizeof(Uint8));
|
||||
|
||||
i = -1;
|
||||
for ( row=0; row<height; ++row )
|
||||
{
|
||||
for ( col=0; col<width; ++col )
|
||||
{
|
||||
if ( col % 8 )
|
||||
{
|
||||
data[i] <<= 1;
|
||||
mask[i] <<= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
++i;
|
||||
data[i] = mask[i] = 0;
|
||||
}
|
||||
switch (xpm[4+row][col])
|
||||
{
|
||||
case 'X':
|
||||
data[i] |= 0x01;
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case '.':
|
||||
mask[i] |= 0x01;
|
||||
break;
|
||||
case ' ':
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
sscanf(xpm[4+row], "%d,%d", &hot_x, &hot_y);
|
||||
cursor = SDL_CreateCursor(data, mask, width, height, hot_x, hot_y);
|
||||
free(data);
|
||||
free(mask);
|
||||
return cursor;
|
||||
}
|
BIN
textures/brick.bmp
Normal file
BIN
textures/brick.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 192 KiB |
BIN
textures/ground.bmp
Normal file
BIN
textures/ground.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 768 KiB |
Loading…
x
Reference in New Issue
Block a user