Starting work on a buffer type.

This commit is contained in:
Kyle Isom 2023-10-09 03:19:09 -07:00
parent 72e3bf77a7
commit d1f84be120
4 changed files with 157 additions and 10 deletions

92
Buffer.cc Normal file
View File

@ -0,0 +1,92 @@
//
// Created by kyle on 2023-10-09.
//
#include <cstring>
#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

35
Buffer.h Normal file
View File

@ -0,0 +1,35 @@
//
// Created by kyle on 2023-10-09.
//
#ifndef KGE_BUFFER_H
#define KGE_BUFFER_H
#include <cstdint>
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

View File

@ -6,10 +6,25 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(SDL2 REQUIRED) find_package(SDL2 REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(Freetype REQUIRED) find_package(Freetype)
if (DEFINED FREETYPE_INCLUDE_DIRS)
add_compile_options("-Wall" "-Wextra" "-Werror")
add_definitions(-DIMGUI_ENABLE_FREETYPE) 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" "$<$<CONFIG:RELEASE>:/O2>")
else()
add_compile_options("-Wall" "-Wextra" "-Werror" "$<$<CONFIG:RELEASE>:-O3>")
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options("-stdlib=libc++")
else()
# nothing special for gcc at the moment
endif()
endif()
add_library(imgui STATIC add_library(imgui STATIC
# Main Imgui files # Main Imgui files
@ -25,25 +40,26 @@ add_library(imgui STATIC
ext/imgui/backends/imgui_impl_opengl3.cpp ext/imgui/backends/imgui_impl_opengl3.cpp
ext/imgui/backends/imgui_impl_opengl3.h ext/imgui/backends/imgui_impl_opengl3.h
ext/imgui/misc/freetype/imgui_freetype.cpp $<IF:$<TARGET_EXISTS:Freetype::Freetype>,${FREETYPE_SOURCES},>)
ext/imgui/misc/freetype/imgui_freetype.h)
add_library(imgui::imgui ALIAS imgui) add_library(imgui::imgui ALIAS imgui)
target_link_libraries(imgui target_link_libraries(imgui
PUBLIC PUBLIC
OpenGL::GL OpenGL::GL
$<IF:$<TARGET_EXISTS:Freetype::Freetype>,Freetype::Freetype,>
$<TARGET_NAME_IF_EXISTS:SDL2::SDL2main> $<TARGET_NAME_IF_EXISTS:SDL2::SDL2main>
$<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static> $<IF:$<TARGET_EXISTS:SDL2::SDL2>,SDL2::SDL2,SDL2::SDL2-static>)
Freetype::Freetype)
target_include_directories(imgui PUBLIC target_include_directories(imgui PUBLIC
ext/imgui/ ext/imgui/
ext/imgui/backends/ ext/imgui/backends/
ext/imgui/misc/freetype ext/imgui/misc/freetype
${FREETYPE_INCLUDE_DIRS}) $<IF:$<TARGET_EXISTS:Freetype::Freetype>,${FREETYPE_INCLUDE_DIRS},>)
include_directories(ext/ ${SDL2_INCLUDE_DIRS}) include_directories(ext/ ${SDL2_INCLUDE_DIRS})
add_executable(kge add_executable(kge
kge.cc kge.cc
Buffer.cc
Buffer.h
) )
target_link_libraries(kge imgui) target_link_libraries(kge imgui)

6
kge.cc
View File

@ -21,8 +21,12 @@ static const float fontPixelSizes[] = {16, 10, 12, 14, 18};
int 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? // Should this use the SDL_EVENTS subsystem?
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
std::cerr << "kge: failed to initialize SDL" << std::endl; std::cerr << "kge: failed to initialize SDL" << std::endl;