Skip to content

Commit 9bead2d

Browse files
committed
Make project compile on linux
1 parent 9223cb0 commit 9bead2d

14 files changed

+760
-364
lines changed

CMakeLists.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
project(tankwars)
3+
4+
add_definitions("-std=c++11")
5+
include_directories(
6+
"ThirdParty/gl3w/include"
7+
"ThirdParty/bullet3/src"
8+
"ThirdParty/glfw3/include"
9+
"ThirdParty/include")
10+
file(GLOB GAME_SRC "Source/Game/*.cpp")
11+
set(GL3W_SRC "ThirdParty/gl3w/src/gl3w.c")
12+
13+
add_subdirectory(ThirdParty/glfw3)
14+
add_subdirectory(ThirdParty/bullet3)
15+
16+
add_executable(tankwars ${GAME_SRC} ${GL3W_SRC})
17+
target_link_libraries(tankwars
18+
glfw
19+
BulletDynamics
20+
BulletCollision
21+
LinearMath
22+
${GLFW_LIBRARIES})
23+

CMakeLists.txt~

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
project(tankwars)
3+
4+
add_definitions("-std=c++1y")
5+
include_directories(
6+
"ThirdParty/gl3w/include"
7+
"ThirdParty/bullet3/src"
8+
"ThirdParty/glfw3/include"
9+
"ThirdParty/include")
10+
file(GLOB GAME_SRC "Source/Game/*.cpp")
11+
set(GL3W_SRC "ThirdParty/gl3w/src/gl3w.c")
12+
13+
add_subdirectory(ThirdParty/glfw3)
14+
add_subdirectory(ThirdParty/bullet3)
15+
16+
add_executable(tankwars ${GAME_SRC} ${GL3W_SRC})
17+
target_link_libraries(tankwars
18+
glfw
19+
BulletDynamics
20+
BulletCollision
21+
LinearMath
22+
${GLFW_LIBRARIES})
23+

Source/Game/GLTools.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sstream>
55
#include <memory>
66
#include <cstring>
7+
#include <stdexcept>
78

89
#include "Image.h"
910

@@ -147,4 +148,4 @@ namespace tankwars {
147148

148149
return false;
149150
}
150-
}
151+
}

Source/Game/GLTools.cpp~

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "GLTools.h"
2+
3+
#include <fstream>
4+
#include <sstream>
5+
#include <memory>
6+
#include <cstring>
7+
8+
#include "Image.h"
9+
10+
namespace tankwars {
11+
GLuint createAndCompileShader(const GLchar* source, GLenum type) {
12+
auto shader = glCreateShader(type);
13+
glShaderSource(shader, 1, &source, nullptr);
14+
glCompileShader(shader);
15+
16+
GLint compileStatus;
17+
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
18+
19+
if (!compileStatus) {
20+
GLint infoLogLength;
21+
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
22+
23+
if (infoLogLength > 0) {
24+
std::unique_ptr<GLchar[]> infoLogBuffer(new GLchar[infoLogLength]);
25+
glGetShaderInfoLog(shader, infoLogLength, nullptr, infoLogBuffer.get());
26+
27+
std::string errorMessage("Failed to compile shader with error message:\n");
28+
errorMessage += infoLogBuffer.get();
29+
throw std::runtime_error(errorMessage);
30+
}
31+
32+
glDeleteShader(shader);
33+
}
34+
35+
return shader;
36+
}
37+
38+
GLuint createShaderFromFile(const std::string& path, GLenum type) {
39+
std::ifstream file(path);
40+
if (!file.is_open()) {
41+
throw std::runtime_error("Failed to open shader-file: " + path);
42+
}
43+
44+
std::stringstream buffer;
45+
buffer << file.rdbuf();
46+
return createAndCompileShader(buffer.str().c_str(), type);
47+
}
48+
49+
GLuint createAndLinkProgram(GLuint vertexShader, GLuint fragmentShader) {
50+
auto program = glCreateProgram();
51+
glAttachShader(program, vertexShader);
52+
glAttachShader(program, fragmentShader);
53+
glLinkProgram(program);
54+
55+
GLint linkStatus;
56+
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
57+
58+
if (!linkStatus) {
59+
GLint infoLogLength;
60+
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
61+
62+
std::unique_ptr<GLchar[]> infoLogBuffer(new GLchar[infoLogLength]);
63+
glGetProgramInfoLog(program, infoLogLength, nullptr, infoLogBuffer.get());
64+
glDeleteProgram(program);
65+
66+
std::string errorMessage("Failed to link program with error message:\n");
67+
errorMessage += infoLogBuffer.get();
68+
throw std::runtime_error(errorMessage);
69+
}
70+
71+
glValidateProgram(program);
72+
73+
GLint validateStatus;
74+
glGetProgramiv(program, GL_VALIDATE_STATUS, &validateStatus);
75+
76+
if (!validateStatus) {
77+
GLint infoLogLength;
78+
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
79+
80+
std::unique_ptr<GLchar[]> infoLogBuffer(new GLchar[infoLogLength]);
81+
glGetProgramInfoLog(program, infoLogLength, nullptr, infoLogBuffer.get());
82+
glDeleteProgram(program);
83+
84+
std::string errorMessage("Failed to validate program with error message:\n");
85+
errorMessage += infoLogBuffer.get();
86+
throw std::runtime_error(errorMessage);
87+
}
88+
89+
return program;
90+
}
91+
92+
GLuint createTextureFromFile(const std::string& path, bool generateMipMaps, bool srgb) {
93+
static const GLint channelToFormat[] = {
94+
GL_R8, GL_RG8, GL_RGB8, GL_RGBA8
95+
};
96+
97+
Image image(path);
98+
GLint format = channelToFormat[image.getNumChannels() - 1];
99+
GLenum textureFormat;
100+
GLenum textureFormatType = GL_UNSIGNED_BYTE;
101+
102+
switch (format) {
103+
case GL_R8: textureFormat = GL_RED; break;
104+
case GL_RG8: textureFormat = GL_RG; break;
105+
case GL_RGB8: textureFormat = GL_RGB; break;
106+
case GL_RGBA8: textureFormat = GL_RGBA; break;
107+
}
108+
109+
if (srgb && textureFormat == GL_RGB8) {
110+
format = GL_SRGB8;
111+
}
112+
else if (srgb && textureFormat == GL_RGBA8) {
113+
format = GL_SRGB8_ALPHA8;
114+
}
115+
116+
if (!textureFormat || !textureFormatType) {
117+
throw std::runtime_error("invalid texture format");
118+
}
119+
120+
GLuint texture;
121+
glGenTextures(1, &texture);
122+
glBindTexture(GL_TEXTURE_2D, texture);
123+
glTexImage2D(GL_TEXTURE_2D, 0, format, image.getWidth(), image.getHeight(), 0,
124+
textureFormat, textureFormatType, image.getImage());
125+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
126+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
127+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, generateMipMaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR);
128+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
129+
130+
if (generateMipMaps) {
131+
glGenerateMipmap(GL_TEXTURE_2D);
132+
}
133+
134+
return texture;
135+
}
136+
137+
bool isExtensionSupported(const std::string& name) {
138+
int numExtensions;
139+
glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
140+
141+
for(int i = 0; i < numExtensions; i++) {
142+
const GLubyte* extStr = glGetStringi(GL_EXTENSIONS, i);
143+
if (strcmp(reinterpret_cast<const char*>(extStr), name.c_str()) == 0 ){
144+
return true;
145+
}
146+
}
147+
148+
return false;
149+
}
150+
}

Source/Game/Game.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22
#include <iostream>
33
#include <random>
4-
#include <GLFW/glfw3.h>
54
#include "VoxelTerrain.h"
5+
#include <GLFW/glfw3.h>
66
#include "Tank.h"
77
#include "Camera.h"
88

@@ -34,4 +34,4 @@ namespace tankwars {
3434
btScalar lastPositionChange = .0f;
3535
btScalar timeBetweenPositionChanges = 3.f;
3636
};
37-
}
37+
}

Source/Game/Game.h~

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <random>
4+
#include <GLFW/glfw3.h>
5+
#include "VoxelTerrain.h"
6+
#include "Tank.h"
7+
#include "Camera.h"
8+
9+
namespace tankwars {
10+
class Game {
11+
public:
12+
Game(Camera * camera,VoxelTerrain* ter);
13+
14+
int setupControllers();
15+
void addCamera(Camera * camera);
16+
void update(float dt);
17+
void render();
18+
void bindControllerToTank(int controllerID, Tank* tank);
19+
void tankGotHit(int index);
20+
private:
21+
btScalar getBestHeightFor(btVector3 pos);
22+
btScalar getBestHeightFor2(btVector3 pos);
23+
bool isPlaneClear(btVector3 vec, int height);
24+
btScalar tankRadius = 1.5f;
25+
btScalar spawnOffset = 17;
26+
btVector3 spawnCoordinates[4];
27+
Tank* tanks[2];
28+
void controller(float dt);
29+
bool closer(btVector3 vec1, btVector3 vec2, glm::vec3 distanceTo);
30+
Camera* camera;
31+
VoxelTerrain* terrain;
32+
int joystickAvailable[2];
33+
float explosion_radius = 3;
34+
btScalar lastPositionChange = .0f;
35+
btScalar timeBetweenPositionChanges = 3.f;
36+
};
37+
}

Source/Game/Image.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stb_image.h>
44
#include <vector>
55
#include <fstream>
6+
#include <stdexcept>
67

78
namespace tankwars {
89
Image::Image(const std::string& path) {
@@ -49,4 +50,4 @@ namespace tankwars {
4950
unsigned char* Image::getImage() const {
5051
return image;
5152
}
52-
}
53+
}

Source/Game/Image.cpp~

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "Image.h"
2+
3+
#include <stb_image.h>
4+
#include <vector>
5+
#include <fstream>
6+
#include <stdexcept>
7+
8+
namespace tankwars {
9+
Image::Image(const std::string& path) {
10+
std::ifstream file(path, std::ios::binary | std::ios::in);
11+
if (!file.is_open()) {
12+
throw std::runtime_error("Attempt to open non-existent image file");
13+
}
14+
15+
file.seekg(0, std::ios::end);
16+
auto size = static_cast<int>(file.tellg());
17+
file.seekg(0, std::ios::beg);
18+
19+
std::vector<char> buffer(size);
20+
file.read(buffer.data(), size);
21+
22+
stbi_set_flip_vertically_on_load(1);
23+
image = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(buffer.data()), size,
24+
&width, &height, &numChannels, STBI_default);
25+
}
26+
27+
Image::Image(const void* data, size_t size) {
28+
image = stbi_load_from_memory(static_cast<const stbi_uc*>(data), static_cast<int>(size),
29+
&width, &height, &numChannels, STBI_default);
30+
}
31+
32+
Image::~Image() {
33+
if (image) {
34+
stbi_image_free(image);
35+
}
36+
}
37+
38+
int Image::getWidth() const {
39+
return width;
40+
}
41+
42+
int Image::getHeight() const {
43+
return height;
44+
}
45+
46+
int Image::getNumChannels() const {
47+
return numChannels;
48+
}
49+
50+
unsigned char* Image::getImage() const {
51+
return image;
52+
}
53+
}

Source/Game/Main.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ int main() {
7272
}
7373

7474
// Init bullet physics
75-
std::unique_ptr<btBroadphaseInterface> broadphase = std::make_unique<btDbvtBroadphase>();
76-
auto collisionConfiguration = std::make_unique<btDefaultCollisionConfiguration>();
77-
auto dispatcher = std::make_unique<btCollisionDispatcher>(collisionConfiguration.get());
78-
auto solver = std::make_unique<btSequentialImpulseConstraintSolver>();
79-
auto dynamicsWorld = std::make_unique<btDiscreteDynamicsWorld>(
80-
dispatcher.get(), broadphase.get(), solver.get(), collisionConfiguration.get());
75+
std::unique_ptr<btBroadphaseInterface> broadphase(new btDbvtBroadphase);
76+
std::unique_ptr<btDefaultCollisionConfiguration> collisionConfiguration(new btDefaultCollisionConfiguration);
77+
std::unique_ptr<btCollisionDispatcher> dispatcher(new btCollisionDispatcher(collisionConfiguration.get()));
78+
std::unique_ptr<btSequentialImpulseConstraintSolver> solver(new btSequentialImpulseConstraintSolver);
79+
std::unique_ptr<btDiscreteDynamicsWorld> dynamicsWorld(new btDiscreteDynamicsWorld(
80+
dispatcher.get(), broadphase.get(), solver.get(), collisionConfiguration.get()));
8181

8282
// Init input systems
8383
tankwars::Keyboard::init();
@@ -276,4 +276,4 @@ int main() {
276276
glfwDestroyWindow(window);
277277
glfwTerminate();
278278
return 0;
279-
}
279+
}

0 commit comments

Comments
 (0)