diff --git a/Buffer.cc b/Buffer.cc new file mode 100644 index 0000000..25e6c8a --- /dev/null +++ b/Buffer.cc @@ -0,0 +1,92 @@ +// +// Created by kyle on 2023-10-09. +// + +#include +#include "Buffer.h" + +namespace kge { +uint8_t * +Buffer::Contents() +{ + return this->contents; +} + +bool +Buffer::Append(uint8_t *data, size_t datalen) +{ + return false; +} + +bool +Buffer::Append(uint8_t c) +{ + return false; +} + +bool +Buffer::Insert(size_t index, uint8_t *data, size_t datalen) +{ + return false; +} + +bool +Buffer::Insert(size_t index, uint8_t c) +{ + return false; +} + +size_t +Buffer::Size() +{ + return this->length; +} + +void +Buffer::Resize(size_t newCapacity) +{ + if (newCapacity < this->length) { + return; + } + + uint8_t *newContents = new uint8_t[newCapacity]; + + memcpy(newContents, this->contents, this->length); + delete this->contents; + this->contents = newContents; + this->capacity = newCapacity; +} + +size_t +Buffer::Trim() +{ + size_t projectedCapacity = this->capacity * 2; + + if (projectedCapacity < this->capacity) { + this->Resize(projectedCapacity); + } +} + +void +Buffer::Clear() +{ + memset(this->contents, 0, this->length); + this->length = 0; +} + +void +Buffer::Reclaim() +{ + delete this->contents; + this->length = 0; + this->capacity = 0; +} + +bool +Buffer::mustGrow(size_t newLength) +{ + return (newLength + this->length) >= this->capacity; +} + + +} // kge \ No newline at end of file diff --git a/Buffer.h b/Buffer.h new file mode 100644 index 0000000..d904ab2 --- /dev/null +++ b/Buffer.h @@ -0,0 +1,35 @@ +// +// Created by kyle on 2023-10-09. +// + +#ifndef KGE_BUFFER_H +#define KGE_BUFFER_H + +#include + + +namespace kge { + +class Buffer { +public: + uint8_t *Contents(); + bool Append(uint8_t *data, size_t datalen); + bool Append(uint8_t c); + bool Insert(size_t index, uint8_t *data, size_t datalen); + bool Insert(size_t index, uint8_t c); + size_t Size(); + void Resize(size_t newCapacity); + size_t Trim(); + void Clear(); + void Reclaim(); +private: + bool mustGrow(size_t newLength); + + uint8_t *contents; + size_t length; + size_t capacity; +}; + +} // kge + +#endif //KGE_BUFFER_H diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d456e5..9ad1dfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,10 +6,25 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(SDL2 REQUIRED) find_package(OpenGL REQUIRED) -find_package(Freetype REQUIRED) +find_package(Freetype) +if (DEFINED FREETYPE_INCLUDE_DIRS) + add_definitions(-DIMGUI_ENABLE_FREETYPE) + set(FREETYPE_SOURCES + ext/imgui/misc/freetype/imgui_freetype.cpp + ext/imgui/misc/freetype/imgui_freetype.h) +endif() + +if(MSVC) + add_compile_options("/W4" "$<$:/O2>") +else() + add_compile_options("-Wall" "-Wextra" "-Werror" "$<$:-O3>") + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + add_compile_options("-stdlib=libc++") + else() + # nothing special for gcc at the moment + endif() +endif() -add_compile_options("-Wall" "-Wextra" "-Werror") -add_definitions(-DIMGUI_ENABLE_FREETYPE) add_library(imgui STATIC # Main Imgui files @@ -25,25 +40,26 @@ add_library(imgui STATIC ext/imgui/backends/imgui_impl_opengl3.cpp ext/imgui/backends/imgui_impl_opengl3.h - ext/imgui/misc/freetype/imgui_freetype.cpp - ext/imgui/misc/freetype/imgui_freetype.h) + $,${FREETYPE_SOURCES},>) add_library(imgui::imgui ALIAS imgui) target_link_libraries(imgui PUBLIC OpenGL::GL + $,Freetype::Freetype,> $ - $,SDL2::SDL2,SDL2::SDL2-static> - Freetype::Freetype) + $,SDL2::SDL2,SDL2::SDL2-static>) target_include_directories(imgui PUBLIC ext/imgui/ ext/imgui/backends/ ext/imgui/misc/freetype - ${FREETYPE_INCLUDE_DIRS}) + $,${FREETYPE_INCLUDE_DIRS},>) include_directories(ext/ ${SDL2_INCLUDE_DIRS}) add_executable(kge kge.cc - ) + Buffer.cc + Buffer.h +) target_link_libraries(kge imgui) diff --git a/kge.cc b/kge.cc index f59f850..43692b0 100644 --- a/kge.cc +++ b/kge.cc @@ -21,8 +21,12 @@ static const float fontPixelSizes[] = {16, 10, 12, 14, 18}; int -main() +main(int argc, char *argv[]) { + // SDL2 requires main have these parameters, but they're not being used + // yet. + (void)argc; + (void)argv; // Should this use the SDL_EVENTS subsystem? if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { std::cerr << "kge: failed to initialize SDL" << std::endl;