Skip to content

Commit

Permalink
Switch to SDL1
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSunCat committed Dec 30, 2021
1 parent 1aa30f0 commit 9e80ce6
Show file tree
Hide file tree
Showing 12 changed files with 773 additions and 112 deletions.
20 changes: 12 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR)

project(Minecraft4k C CXX)

include_directories(include build)
include_directories(include)

set(PROJECT_NAME Minecraft4k)

Expand All @@ -24,13 +24,13 @@ set(Resource_Files
source_group("Resource Files" FILES ${Resource_Files})

set(Source_Files
"glad.c"
"Minecraft4k.cpp"
"Shader.cpp"
"TextureGenerator.cpp"
"Util.cpp"
"World.cpp"
"Vector.cpp"
"glad.c"
)
source_group("Source Files" FILES ${Source_Files})

Expand All @@ -40,7 +40,11 @@ set(ALL_FILES
${Source_Files}
)

find_package(SDL2 REQUIRED)
# Don't link stdlib
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")

find_package(SDL REQUIRED)

add_executable(${PROJECT_NAME} ${ALL_FILES})
set(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
Expand All @@ -58,16 +62,16 @@ if(UNIX)
# target_compile_options(${PROJECT_NAME} PRIVATE -g -DDEBUG)

# TODO -m32, use C libs
target_compile_options(${PROJECT_NAME} PRIVATE
-Os -s -fno-rtti -fno-stack-protector -ffunction-sections -fdata-sections -Wl,--gc-sections
-fno-math-errno -fno-unroll-loops -fmerge-all-constants -fno-ident -mfpmath=387 -mfancy-math-387
target_compile_options(${PROJECT_NAME} PRIVATE -DSDL_stdinc_h_
-Os -s -fno-rtti -fno-stack-protector -ffunction-sections -fdata-sections -fno-math-errno
-fno-unroll-loops -fmerge-all-constants -fno-ident -mfpmath=387 -mfancy-math-387 #-e _start
-fsingle-precision-constant -ffast-math -fno-exceptions -nostartfiles -nostdlib -nodefaultlibs
-fcf-protection=none -N -fno-align-functions -fomit-frame-pointer -fno-threadsafe-statics
-finline-limit=10 -fno-asynchronous-unwind-tables
)

target_link_options(${PROJECT_NAME} PRIVATE
-Wl,--hash-style=gnu,--build-id=none,-z,max-page-size=2048
-Wl,--hash-style=gnu,--build-id=none,--gc-sections#,-z,max-page-size=2048#,-e,_start
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
Expand Down Expand Up @@ -118,7 +122,7 @@ set(ADDITIONAL_LIBRARY_DEPENDENCIES
)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE "${ADDITIONAL_LIBRARY_DEPENDENCIES}" ${SDL2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PRIVATE "${ADDITIONAL_LIBRARY_DEPENDENCIES} ${SDL_LIBRARY}")

link_directories(${PROJECT_NAME} PRIVATE
"lib"
Expand Down
3 changes: 2 additions & 1 deletion Constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
typedef unsigned char uint8_t;
typedef unsigned long uint64_t;

#include "Vector.h"

Expand Down
88 changes: 35 additions & 53 deletions Minecraft4k.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <cmath>

#include <glad/glad.h>
#include <SDL2/SDL.h>
#include <glad.h>
#include <SDL/SDL.h>

#include "Constants.h"
#include "Shader.h"
Expand All @@ -11,8 +9,6 @@

#include "shader_code.h"

SDL_GLContext mainContext;

struct Controller
{
float forward;
Expand Down Expand Up @@ -88,11 +84,11 @@ static vec3 lerp(const vec3& start, const vec3& end, const float t)
}

void initTexture(GLuint* texture, const int width, const int height);
void updateMouse(SDL_Window* window);
void updateController(SDL_Window* window);
void updateMouse();
void updateController();
void onWindowResized(int width, int height);

void updateScreenResolution(SDL_Window* window)
void updateScreenResolution()
{
if (SCR_DETAIL < -4)
SCR_DETAIL = -4;
Expand Down Expand Up @@ -139,7 +135,7 @@ void updateScreenResolution(SDL_Window* window)
break;
}

SDL_SetWindowTitle(window, title);
SDL_WM_SetCaption(title, nullptr);

glDeleteTextures(1, &screenTexture);
initTexture(&screenTexture, int(SCR_RES.x), int(SCR_RES.y));
Expand Down Expand Up @@ -242,11 +238,11 @@ void collidePlayer()
//prints(playerPos); prints('\n');
}

void run(SDL_Window* window) {
void run() {
auto lastUpdateTime = currentTime();
float lastFrameTime = lastUpdateTime - 16;

SDL_WarpMouseInWindow(window, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
SDL_WarpMouse(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

SDL_Event evt;

Expand All @@ -256,11 +252,11 @@ void run(SDL_Window* window) {
deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;

updateMouse(window);
updateController(window);
updateMouse();
updateController();

if (needsResUpdate) {
updateScreenResolution(window);
updateScreenResolution();
}

sinYaw = sin(cameraYaw);
Expand Down Expand Up @@ -377,7 +373,7 @@ void run(SDL_Window* window) {

prints("frame\n");

SDL_GL_SwapWindow(window);
SDL_GL_SwapBuffers();

while(SDL_PollEvent(&evt))
{
Expand All @@ -392,16 +388,16 @@ void run(SDL_Window* window) {
//glfwTerminate();
}

void updateMouse(SDL_Window* window)
void updateMouse()
{
int x, y;
SDL_GetMouseState(&x, &y);
SDL_WarpMouseInWindow(window, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
SDL_WarpMouse(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);

cameraYaw += (x - (WINDOW_WIDTH / 2)) / 500.0f;
cameraPitch -= (y - (WINDOW_HEIGHT / 2)) / 500.0f;

if(fabs(cameraYaw) > PI)
if(abs(cameraYaw) > PI)
{
if (cameraYaw > 0)
cameraYaw = -PI - (cameraYaw - PI);
Expand Down Expand Up @@ -458,28 +454,28 @@ bool keyPress(SDL_Window* window, int key)
return false;
}*/

void updateController(SDL_Window* window)
void updateController()
{
controller.reset();

const uint8_t *keyboard = SDL_GetKeyboardState(nullptr);
const uint8_t *keyboard = SDL_GetKeyState(nullptr);

if(keyboard[SDL_SCANCODE_W])
if(keyboard[SDLK_w])
controller.forward += 1.0f;
if (keyboard[SDL_SCANCODE_S])
if (keyboard[SDLK_s])
controller.forward -= 1.0f;
if (keyboard[SDL_SCANCODE_D])
if (keyboard[SDLK_d])
controller.right += 1.0f;
if (keyboard[SDL_SCANCODE_A])
if (keyboard[SDLK_a])
controller.right -= 1.0f;
if (keyboard[SDL_SCANCODE_SPACE])
if (keyboard[SDLK_SPACE])
controller.jump = true;

if (keyboard[SDL_SCANCODE_COMMA]) {
if (keyboard[SDLK_COMMA]) {
SCR_DETAIL--;
needsResUpdate = true;
}
if (keyboard[SDL_SCANCODE_PERIOD]) {
if (keyboard[SDLK_PERIOD]) {
SCR_DETAIL++;
needsResUpdate = true;
}
Expand Down Expand Up @@ -525,53 +521,39 @@ void initTexture(GLuint* texture, const int width, const int height) {
}

// TODO maybe use _start: https://int21.de/linux4k/
int main(const int argc, const char** argv)
int main()
{
prints("Initializing SDL... ");
// uhh I guess we don't need to SDL_Init??
prints("Done!\n");

prints("Creating window... ");

SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_OPENGL); // TODO SDL_FULLSCREEN?
SDL_ShowCursor(SDL_DISABLE);

// Request an OpenGL 4.3 context (should be core)
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // TODO what is this?
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
/*SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);*/

#ifdef __APPLE__
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); // add on Mac bc Apple is big dumb :(
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // add on Mac bc Apple is big dumb :(
#endif

SDL_Window* window = SDL_CreateWindow("Minecraft4k", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL);

SDL_SetWindowResizable(window, SDL_TRUE);

if(!window)
{
prints("Failed to create window!\n");
return -1;
}
prints("Done!\n");

SDL_ShowCursor(SDL_FALSE);

prints("Setting OpenGL context... ");
mainContext = SDL_GL_CreateContext(window);
if(!mainContext)
{
prints("Failed to create OpenGL context!\n");
return -1;
}


prints("Done!\n");

prints("Loading OpenGL functions... ");
gladLoadGLLoader(SDL_GL_GetProcAddress);
prints("Done!\n");

// enable vsync so we don't run at about a kjghpillion fps
SDL_GL_SetSwapInterval(1);
// TODO enable vsync so we don't run at about a kjghpillion fps
//SDL_GL_SetSwapState(1);

prints("Configuring OpenGL... ");

Expand Down Expand Up @@ -613,7 +595,7 @@ int main(const int argc, const char** argv)
init();
prints("Finished initializing engine! Running the game...\n");

run(window);
run();

SDL_ShowCursor(SDL_TRUE);
}
2 changes: 0 additions & 2 deletions Shader.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "Shader.h"

#include <cstring>

#include "Util.h"

Shader::Shader(const char* vertexCode, const char* fragmentCode)
Expand Down
2 changes: 1 addition & 1 deletion Shader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <glad/glad.h>
#include <glad.h>
#include <vector>

#include "Vector.h"
Expand Down
2 changes: 1 addition & 1 deletion TextureGenerator.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <glad/glad.h>
#include <glad.h>

GLuint generateTextures(long long seed);
Loading

0 comments on commit 9e80ce6

Please sign in to comment.