Compare commits

..

2 Commits

Author SHA1 Message Date
Shylie
c8e7a28837 Remove testing code from CMake. Intended to be used via CI gitea actions 2024-05-25 23:18:09 -04:00
Shylie
b4427a0c88 Setup basic testing framework 2024-05-25 23:16:23 -04:00
12 changed files with 1867 additions and 3 deletions

View File

@ -6,6 +6,7 @@ project(glerminal
) )
option(GLERMINAL_OPENGL_DEBUG_CONTEXT "" OFF) option(GLERMINAL_OPENGL_DEBUG_CONTEXT "" OFF)
option(GLERMINAL_TEST "" OFF)
set(GLERMINAL_GRID_WIDTH 40 CACHE STRING "") set(GLERMINAL_GRID_WIDTH 40 CACHE STRING "")
set(GLERMINAL_GRID_HEIGHT 25 CACHE STRING "") set(GLERMINAL_GRID_HEIGHT 25 CACHE STRING "")
set(GLERMINAL_LAYER_COUNT 64 CACHE STRING "") set(GLERMINAL_LAYER_COUNT 64 CACHE STRING "")
@ -62,4 +63,8 @@ endif()
if (PROJECT_IS_TOP_LEVEL) if (PROJECT_IS_TOP_LEVEL)
add_subdirectory(examples examples) add_subdirectory(examples examples)
endif()
if (GLERMINAL_TEST)
add_subdirectory(tests tests)
endif() endif()

View File

@ -1,6 +1,7 @@
#include <glerminal.h> #include <glerminal.h>
#include <cstdlib> #include <cstdlib>
#include <cmath>
namespace namespace
{ {

View File

@ -7,6 +7,8 @@ namespace
{ {
void init() void init()
{ {
srand(0);
glerminal_load_sprites_file("resources/towers.png"); glerminal_load_sprites_file("resources/towers.png");
for (int i = 0; i < LAYER_COUNT; i++) for (int i = 0; i < LAYER_COUNT; i++)

View File

@ -18,6 +18,8 @@ typedef void (*glerminal_main_cb)(float dt);
*/ */
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main); void glerminal_run(glerminal_init_cb init, glerminal_main_cb main);
void glerminal_quit();
/** /**
* @brief Update the displayed screen contents to the current state of the library * @brief Update the displayed screen contents to the current state of the library
*/ */

View File

@ -1,5 +1,5 @@
#ifndef TERMG_PRIVATE_H #ifndef GLERMINAL_PRIVATE_H
#define TERMG_PRIVATE_H #define GLERMINAL_PRIVATE_H
#include "glerminal.h" #include "glerminal.h"
@ -34,6 +34,7 @@ namespace glerminal
glerminal& operator=(glerminal&&) = delete; glerminal& operator=(glerminal&&) = delete;
void run(); void run();
void quit();
void flush(); void flush();
@ -98,4 +99,4 @@ namespace glerminal
}; };
} }
#endif//TERMG_PRIVATE_H #endif//GLERMINAL_PRIVATE_H

View File

@ -194,6 +194,11 @@ namespace glerminal
} }
} }
void glerminal::quit()
{
glfwSetWindowShouldClose(m_window, GLFW_TRUE);
}
void glerminal::flush() void glerminal::flush()
{ {
glNamedBufferData(m_sprites_instance_vbo, sizeof(m_cells), m_cells, GL_STREAM_DRAW); glNamedBufferData(m_sprites_instance_vbo, sizeof(m_cells), m_cells, GL_STREAM_DRAW);
@ -664,6 +669,13 @@ void glerminal_run(glerminal_init_cb init, glerminal_main_cb main)
} }
} }
void glerminal_quit()
{
if (!GLERMINAL_G) { return; }
GLERMINAL_G->quit();
}
void glerminal_flush() void glerminal_flush()
{ {
if (!GLERMINAL_G) { return; } if (!GLERMINAL_G) { return; }

56
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.28)
set(CMAKE_FOLDER tests)
add_library(test-common STATIC
${CMAKE_SOURCE_DIR}/source/glad/glad.h
test-common/test-common.h
test-common/stb_image_write.h
test-common/test-common.cpp
)
target_include_directories(test-common
PUBLIC
test-common
PRIVATE
${CMAKE_SOURCE_DIR}/source
)
target_link_libraries(test-common PRIVATE glerminal)
file(GLOB_RECURSE
TEST_RESOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/resources/**.png
)
foreach(RESOURCE_FILE ${TEST_RESOURCES})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${RESOURCE_FILE}
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_FILE}
${CMAKE_CURRENT_BINARY_DIR}/${RESOURCE_FILE}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/${RESOURCE_FILE}
)
endforeach()
file(GLOB_RECURSE
TEST_SOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)
list(REMOVE_ITEM TEST_SOURCES test-common/test-common.cpp)
list(TRANSFORM TEST_RESOURCES PREPEND ${CMAKE_CURRENT_BINARY_DIR}/)
foreach(SOURCE_FILE ${TEST_SOURCES})
get_filename_component(SOURCE_FILENAME ${SOURCE_FILE} NAME_WLE)
add_executable(test-${SOURCE_FILENAME} WIN32 ${SOURCE_FILE} ${TEST_RESOURCES})
target_link_libraries(test-${SOURCE_FILENAME} PRIVATE glerminal test-common)
endforeach()

31
tests/basic.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <glerminal.h>
#include <test-common.h>
namespace
{
void init()
{
glerminal_load_sprites_file("resources/image.png");
for (int i = 0; i < GRID_HEIGHT; i++)
{
glerminal_set(i, i, 0, 1);
}
glerminal_flush();
glerminal_test_save_image();
glerminal_quit();
}
void mainloop(float) {}
}
int main()
{
glerminal_run(init, mainloop);
return 0;
}

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:05ec57e986f3fbd9efcc6c9f8e571393af4db90d8eb92f041990ed690bddaf89
size 196

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
#include <test-common.h>
#include <glerminal.h>
#include <glad/glad.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
namespace
{
unsigned char pixels[1280 * 800 * 3];
}
void glerminal_test_save_image()
{
glReadBuffer(GL_LEFT);
glReadPixels(0, 0, GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, GL_RGB, GL_UNSIGNED_BYTE, pixels);
stbi_flip_vertically_on_write(true);
stbi_write_png("image.png", GRID_WIDTH * CELL_SCALE * 8, GRID_HEIGHT * CELL_SCALE * 8, 3, pixels, 1280 * 3);
}

View File

@ -0,0 +1,6 @@
#ifndef GLERMINAL_TEST_COMMON_H
#define GLERMINAL_TEST_COMMON_H
void glerminal_test_save_image();
#endif//GLERMINAL_TEST_COMMON_H