Skip to content

Commit

Permalink
Hello GLFW
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Oct 29, 2023
1 parent a91c51d commit 4df39fa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Expand Up @@ -5,5 +5,8 @@ project(pt-playground)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(external)

add_executable(pt src/main.cpp)
target_link_libraries(pt PRIVATE glfw)
set_target_properties(pt PROPERTIES COMPILE_WARNING_AS_ERROR ON)
12 changes: 12 additions & 0 deletions external/CMakeLists.txt
@@ -0,0 +1,12 @@
include(FetchContent)

# Declare
FetchContent_Declare(glfw
GIT_REPOSITORY "https://github.com/glfw/glfw"
GIT_TAG 7482de6) # 3.3.8

# Fetch
message(STATUS "Fetching glfw...")
FetchContent_MakeAvailable(glfw)

# Configure
46 changes: 45 additions & 1 deletion src/main.cpp
@@ -1,6 +1,50 @@
#include <chrono>
#include <cstdio>

#include <thread>

#include <GLFW/glfw3.h>

inline constexpr int defaultWindowWidth = 640;
inline constexpr int defaultWindowHeight = 480;

int main()
{
std::printf("Hello, world\n");
if (!glfwInit())
{
std::fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}

GLFWwindow* const window = glfwCreateWindow(
defaultWindowWidth, defaultWindowHeight, "pt-playground 🛝", nullptr, nullptr);
if (!window)
{
std::fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return -1;
}

{
glfwMakeContextCurrent(window);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
}

glfwSwapBuffers(window);

std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
}

glfwDestroyWindow(window);
glfwTerminate();

return 0;
}

0 comments on commit 4df39fa

Please sign in to comment.