Skip to content

Commit ac8eafa

Browse files
authored
Merge pull request #20 from matty316/controller
Controller
2 parents 4e065c4 + 9496767 commit ac8eafa

File tree

371 files changed

+144445
-10
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

371 files changed

+144445
-10
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
cmake_minimum_required(VERSION 3.13)
22

33
project(opeGL)
4+
5+
add_subdirectory("thirdParty/glfw")
6+
47
set(CMAKE_CXX_STANDARD 23)
8+
59
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
610
file(GLOB_RECURSE MY_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
711
file(GLOB_RECURSE MY_RESOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/resources/*")
812

913
add_library(opeGL "${MY_SOURCES}" "${MY_HEADERS}" "${MY_RESOURCES}" "${CMAKE_CURRENT_SOURCE_DIR}/src/glad.c")
1014
target_include_directories(opeGL PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/")
11-
target_link_libraries(opeGL PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libglfw3.a)
1215
target_link_libraries(opeGL PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libassimp.a)
1316
target_link_libraries(opeGL PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libtmxlite.a)
1417
target_link_libraries(opeGL PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdParty/libzlibstatic.a)
18+
target_link_libraries(opeGL PUBLIC glfw)
1519
target_compile_options(opeGL PRIVATE -Wall -Wextra -g)

include/camera.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class OpeCamera {
1616
glm::mat4 getView();
1717
glm::vec3 getPosition();
1818
void setPlayerPos(glm::vec2 pos);
19+
void updateRightAxes(double deltaTime, float x, float y);
1920

2021
struct Movement {
2122
bool forward = false;

include/opegl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <GLFW/glfw3.h>
66
// clang-format on
77

8+
#include <memory>
89
#include <string>
910
#include <vector>
1011

@@ -41,7 +42,7 @@ class OpeGL {
4142
std::vector<PointLight> pointLights;
4243
std::vector<OpeModel> models;
4344

44-
OpeLevel *currentLevel = nullptr;
45+
std::unique_ptr<OpeLevel> currentLevel;
4546

4647
double timeStamp = glfwGetTime();
4748
double deltaTime = 0.0f;

shaders/shader.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void main() {
3939
lighting += BlinnPhong(norm, fragPos, pointLights[i].position, pointLights[i].diffuse);
4040
color *= lighting;
4141

42-
//color = pow(color, 1/gamma);
42+
color = pow(color, 1/gamma);
4343
FragColor = vec4(color, 1.0);
4444
}
4545

src/camera.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ void OpeCamera::setUpVector(glm::vec3 up) {
6969
}
7070

7171
void OpeCamera::resetMousePosition(const glm::vec2 &p) { mousePosition = p; }
72+
73+
void OpeCamera::updateRightAxes(double deltaTime, float x, float y) {
74+
auto newX = glm::abs(x) > 0.5f ? x : 0;
75+
auto newY = glm::abs(y) > 0.5f ? y : 0;
76+
auto newQuat = glm::quat(glm::vec3(newY * deltaTime, newX * deltaTime, 0.0f));
77+
cameraOrientation = glm::normalize(newQuat * cameraOrientation);
78+
setUpVector(worldUp);
79+
}

src/opegl.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#include "opegl.hpp"
22
#include "GLFW/glfw3.h"
33
#include "constants.hpp"
4+
#include "glm/common.hpp"
45
#include "glm/ext/matrix_clip_space.hpp"
56
#include "glm/ext/vector_float2.hpp"
67
#include "glm/trigonometric.hpp"
78
#include "level.hpp"
89
#include "light.hpp"
910
#include "shader.hpp"
1011
#include "vertex.hpp"
12+
#include <cfloat>
1113
#include <cstddef>
1214
#include <cstdint>
15+
#include <cstdlib>
1316
#include <iostream>
17+
#include <memory>
18+
#include <print>
1419
#include <stdexcept>
1520

1621
OpeGL::OpeGL() { init(); }
@@ -37,11 +42,11 @@ void OpeGL::mainLoop() {
3742
}
3843

3944
while (!glfwWindowShouldClose(window)) {
45+
processInput(window);
4046
update();
4147
int width, height;
4248
glfwGetFramebufferSize(window, &width, &height);
4349
glViewport(0, 0, width, height);
44-
processInput(window);
4550

4651
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
4752
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -54,6 +59,7 @@ void OpeGL::mainLoop() {
5459
shader.setMat4("projection", projection);
5560

5661
shader.setMat4("view", camera.getView());
62+
shader.setVec3("viewPos", camera.getPosition());
5763

5864
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, perInstanceDataBuffer);
5965

@@ -65,6 +71,7 @@ void OpeGL::mainLoop() {
6571
modelShader.setMat4("projection", projection);
6672

6773
modelShader.setMat4("view", camera.getView());
74+
modelShader.setVec3("viewPos", camera.getPosition());
6875

6976
for (auto &model : models) {
7077
modelShader.setMat4("modelMatrix", model.modelMatrix());
@@ -85,6 +92,7 @@ void OpeGL::init() {
8592
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
8693
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
8794
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
95+
8896
if (debug)
8997
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
9098
window =
@@ -173,14 +181,48 @@ void OpeGL::framebuffer_size_callback(GLFWwindow *window, int width,
173181
}
174182

175183
void OpeGL::processInput(GLFWwindow *window) {
176-
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
177-
glfwSetWindowShouldClose(window, true);
184+
if (glfwJoystickPresent(GLFW_JOYSTICK_1)) {
185+
if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) {
186+
GLFWgamepadstate state;
187+
if (glfwGetGamepadState(GLFW_JOYSTICK_1, &state)) {
188+
float left_stick_x = state.axes[GLFW_GAMEPAD_AXIS_LEFT_X];
189+
float left_stick_y = state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
190+
float right_stick_x = state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X];
191+
float right_stick_y = state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];
192+
193+
camera.movement.forward = left_stick_y < -0.5f;
194+
camera.movement.backward = left_stick_y > 0.5f;
195+
camera.movement.left = left_stick_x < -0.5f;
196+
camera.movement.right = left_stick_x > 0.5f;
197+
198+
camera.updateRightAxes(deltaTime, right_stick_x, right_stick_y);
199+
}
200+
} else {
201+
int count;
202+
const float *axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count);
203+
204+
for (int i = 0; i < count; i++)
205+
std::println("axes {} == {}", i, axes[i]);
206+
207+
if (count >= 4) {
208+
camera.movement.forward = axes[1] < -0.5f;
209+
camera.movement.backward = axes[1] > 0.5f;
210+
camera.movement.left = axes[0] < -0.5f;
211+
camera.movement.right = axes[0] > 0.5f;
212+
213+
camera.updateRightAxes(deltaTime, axes[4], axes[3]);
214+
}
215+
}
216+
}
178217
}
179218

180219
void OpeGL::key_callback(GLFWwindow *window, int key, int scancode, int action,
181220
int mods) {
182221
auto app = reinterpret_cast<OpeGL *>(glfwGetWindowUserPointer(window));
183222
const bool press = action != GLFW_RELEASE;
223+
224+
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
225+
glfwSetWindowShouldClose(window, true);
184226
if (key == GLFW_KEY_ESCAPE)
185227
glfwSetWindowShouldClose(window, GLFW_TRUE);
186228
if (key == GLFW_KEY_W)
@@ -309,9 +351,9 @@ void OpeGL::addPointLight(PointLight &light) { pointLights.push_back(light); }
309351
void OpeGL::loadLevel(std::string path, uint32_t wallTexture,
310352
uint32_t floorTexture, uint32_t ceilingTexture,
311353
size_t maxHeight) {
312-
auto newLevel =
313-
OpeLevel(path, wallTexture, floorTexture, ceilingTexture, maxHeight);
314-
currentLevel = &newLevel;
354+
auto newLevel = std::make_unique<OpeLevel>(
355+
OpeLevel(path, wallTexture, floorTexture, ceilingTexture, maxHeight));
356+
currentLevel = std::move(newLevel);
315357
currentLevel->loadLevel(*this);
316358
}
317359

src/texture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ size_t OpeTexture::loadTexture(const std::string &filename) {
3030
GL_LINEAR_MIPMAP_LINEAR);
3131
glTextureParameteri(textureID, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
3232

33-
glTextureStorage2D(textureID, 1, GL_RGBA8, width, height);
33+
glTextureStorage2D(textureID, 1, GL_RGBA16F, width, height);
3434
glTextureSubImage2D(textureID, 0, 0, 0, width, height, format,
3535
GL_UNSIGNED_BYTE, data);
3636

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Usage:
2+
# cmake -P GenerateMappings.cmake <path/to/mappings.h.in> <path/to/mappings.h>
3+
4+
set(source_url "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt")
5+
set(source_path "${CMAKE_CURRENT_BINARY_DIR}/gamecontrollerdb.txt")
6+
set(template_path "${CMAKE_ARGV3}")
7+
set(target_path "${CMAKE_ARGV4}")
8+
9+
if (NOT EXISTS "${template_path}")
10+
message(FATAL_ERROR "Failed to find template file ${template_path}")
11+
endif()
12+
13+
file(DOWNLOAD "${source_url}" "${source_path}"
14+
STATUS download_status
15+
TLS_VERIFY on)
16+
17+
list(GET download_status 0 status_code)
18+
list(GET download_status 1 status_message)
19+
20+
if (status_code)
21+
message(FATAL_ERROR "Failed to download ${source_url}: ${status_message}")
22+
endif()
23+
24+
file(STRINGS "${source_path}" lines)
25+
foreach(line ${lines})
26+
if (line MATCHES "^[0-9a-fA-F]")
27+
if (line MATCHES "platform:Windows")
28+
if (GLFW_WIN32_MAPPINGS)
29+
string(APPEND GLFW_WIN32_MAPPINGS "\n")
30+
endif()
31+
string(APPEND GLFW_WIN32_MAPPINGS "\"${line}\",")
32+
elseif (line MATCHES "platform:Mac OS X")
33+
if (GLFW_COCOA_MAPPINGS)
34+
string(APPEND GLFW_COCOA_MAPPINGS "\n")
35+
endif()
36+
string(APPEND GLFW_COCOA_MAPPINGS "\"${line}\",")
37+
elseif (line MATCHES "platform:Linux")
38+
if (GLFW_LINUX_MAPPINGS)
39+
string(APPEND GLFW_LINUX_MAPPINGS "\n")
40+
endif()
41+
string(APPEND GLFW_LINUX_MAPPINGS "\"${line}\",")
42+
endif()
43+
endif()
44+
endforeach()
45+
46+
configure_file("${template_path}" "${target_path}" @ONLY NEWLINE_STYLE UNIX)
47+
file(REMOVE "${source_path}")
48+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleExecutable</key>
8+
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
9+
<key>CFBundleGetInfoString</key>
10+
<string>${MACOSX_BUNDLE_INFO_STRING}</string>
11+
<key>CFBundleIconFile</key>
12+
<string>${MACOSX_BUNDLE_ICON_FILE}</string>
13+
<key>CFBundleIdentifier</key>
14+
<string>${MACOSX_BUNDLE_GUI_IDENTIFIER}</string>
15+
<key>CFBundleInfoDictionaryVersion</key>
16+
<string>6.0</string>
17+
<key>CFBundleLongVersionString</key>
18+
<string>${MACOSX_BUNDLE_LONG_VERSION_STRING}</string>
19+
<key>CFBundleName</key>
20+
<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
21+
<key>CFBundlePackageType</key>
22+
<string>APPL</string>
23+
<key>CFBundleShortVersionString</key>
24+
<string>${MACOSX_BUNDLE_SHORT_VERSION_STRING}</string>
25+
<key>CFBundleSignature</key>
26+
<string>????</string>
27+
<key>CFBundleVersion</key>
28+
<string>${MACOSX_BUNDLE_BUNDLE_VERSION}</string>
29+
<key>CSResourcesFileMapped</key>
30+
<true/>
31+
<key>LSRequiresCarbon</key>
32+
<true/>
33+
<key>NSHumanReadableCopyright</key>
34+
<string>${MACOSX_BUNDLE_COPYRIGHT}</string>
35+
<key>NSHighResolutionCapable</key>
36+
<true/>
37+
</dict>
38+
</plist>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
if (NOT EXISTS "@GLFW_BINARY_DIR@/install_manifest.txt")
3+
message(FATAL_ERROR "Cannot find install manifest: \"@GLFW_BINARY_DIR@/install_manifest.txt\"")
4+
endif()
5+
6+
file(READ "@GLFW_BINARY_DIR@/install_manifest.txt" files)
7+
string(REGEX REPLACE "\n" ";" files "${files}")
8+
9+
foreach (file ${files})
10+
message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
11+
if (EXISTS "$ENV{DESTDIR}${file}")
12+
exec_program("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
13+
OUTPUT_VARIABLE rm_out
14+
RETURN_VALUE rm_retval)
15+
if (NOT "${rm_retval}" STREQUAL 0)
16+
MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
17+
endif()
18+
elseif (IS_SYMLINK "$ENV{DESTDIR}${file}")
19+
EXEC_PROGRAM("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
20+
OUTPUT_VARIABLE rm_out
21+
RETURN_VALUE rm_retval)
22+
if (NOT "${rm_retval}" STREQUAL 0)
23+
message(FATAL_ERROR "Problem when removing symlink \"$ENV{DESTDIR}${file}\"")
24+
endif()
25+
else()
26+
message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
27+
endif()
28+
endforeach()
29+

0 commit comments

Comments
 (0)