Skip to content

Commit

Permalink
creating a stub for all public interface functions
Browse files Browse the repository at this point in the history
creating some global utilities for core and a few creation stubs
DEBUG define for debug builds
reuse default_int as a way to index SETSTR variables
  • Loading branch information
bbbradsmith committed Apr 21, 2024
1 parent cf770ad commit 84eb267
Show file tree
Hide file tree
Showing 14 changed files with 708 additions and 34 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>DEBUG;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalIncludeDirectories>..\include\</AdditionalIncludeDirectories>
Expand Down Expand Up @@ -134,7 +134,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>DEBUG;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalIncludeDirectories>..\include\</AdditionalIncludeDirectories>
Expand Down
100 changes: 99 additions & 1 deletion core/core.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,103 @@
// core.cpp
// high level central operation of core functions
// High level central operation of core functions.
// Implementation of NSFCore members.
// Global utilities.

#include "core.h"
#include "enums_data.h"

#include <cstdio> // std::fprintf, std::vsnprintf, stdout, stderr
#include <cstdlib> // std::malloc, std::free, std::exit
#include <cstring> // std:;memset, std::memcpy
#include <cstdarg> // va_list, va_start

namespace nsfp {

void (*debug_print_callback)(const char* msg) = NULL;
void (*fatal_callback)(const char* msg) = NULL;

void* alloc(size_t size)
{
void* a = std::malloc(size);
if (a == NULL) nsfp::fatal("Out of memory.");
NSFP_DEBUG("alloc(%z)=%p",size,a);
return a;
}

void free(void* ptr)
{
NSFP_DEBUG("free(%p)",ptr);
std::free(ptr);
}

void debug(const char* msg)
{
if (debug_print_callback) debug_print_callback(msg);
else std::fprintf(stdout,"%s\n",msg);
}

void debug_printf(const char* fmt,...)
{
#ifdef DEBUG
static char msg[1024];
va_list args;
va_start(args,fmt);
std::vsnprintf(msg,sizeof(msg),msg,args);
#else
(void)fmt;
#endif
}

void fatal(const char* msg)
{
if (fatal_callback) fatal_callback(msg);
std::fprintf(stderr,"%s\n",msg);
std::exit(-1);
}

} // namespace nsfp

NSFCore* NSFCore::create()
{
NSFCore* core = (NSFCore*)nsfp::alloc(sizeof(NSFCore));
NSFP_DEBUG("create()=%p",core);
std::memset(core,0,sizeof(NSFCore));
core->set_default();
return core;
}

void NSFCore::destroy(NSFCore* core)
{
NSFP_DEBUG("destroy(%p)",core);
core->release();
nsfp::free(core);
}

void NSFCore::finalize()
{
// TODO make any allocations needed based on the settings
}

void NSFCore::release()
{
// TODO release any allocations
}

void NSFCore::set_default()
{
// TODO
}

bool NSFCore::set_ini(const char* ini)
{
(void)ini;
// TODO
return false;
}

bool NSFCore::set_init(const NSFSetInit* init)
{
(void)init;
// TODO
return false;
}
42 changes: 42 additions & 0 deletions core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,46 @@ typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;

#ifdef DEBUG
#define NSFP_DEBUG(...) { nsfp::debug_printf(__VA_ARGS__); }
#else
#define NSFP_DEBUG(...) {}
#endif

// NSFCore structure, code members defined in core.cpp

typedef struct NSFCore_
{
sint32 set[NSFP_SET_COUNT]; // all integer settings (values for string settings will index set_str)
const char* set_str[NSFP_SETSTR_COUNT];
bool set_str_free[NSFP_SETSTR_COUNT]; // true if string setting

static NSFCore* create(); // After create: ->set_... then ->finalize.
static void destroy(NSFCore* core); // Calls ->release before freeing the core.
void finalize(); // finishes creation after create and initial settings
void release(); // called by destroy, releases all owned allocations

void set_default();
bool set_ini(const char* ini);
bool set_init(const NSFSetInit* init);

} NSFCore;

namespace nsfp {

// core.cpp

extern "C" {
extern void (*debug_print_callback)(const char* msg);
extern void (*fatal_callback)(const char* msg);
}

void* alloc(size_t size);
void free(void* ptr);
void debug_printf(const char* fmt,...); // only works if DEBUG defined
void debug(const char* msg);
void fatal(const char* msg);

} // namespace nsfp

#endif // __CORE_PCH__
4 changes: 2 additions & 2 deletions core/core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>DEBUG;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>core.h</PrecompiledHeaderFile>
Expand Down Expand Up @@ -140,7 +140,7 @@
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>DEBUG;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>core.h</PrecompiledHeaderFile>
Expand Down
Loading

0 comments on commit 84eb267

Please sign in to comment.