Skip to content

Commit

Permalink
Add MOJOSHADER_USE_SDL_STDLIB, to avoid C runtime dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Apr 1, 2020
1 parent aafeedb commit 9a5fdb1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions mojoshader_common.c
@@ -1,6 +1,8 @@
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
#ifndef MOJOSHADER_USE_SDL_STDLIB
#include <math.h>
#endif /* MOJOSHADER_USE_SDL_STDLIB */

// Convenience functions for allocators...
#if !MOJOSHADER_FORCE_ALLOCATOR
Expand Down
2 changes: 2 additions & 0 deletions mojoshader_effects.c
Expand Up @@ -12,7 +12,9 @@

#ifdef MOJOSHADER_EFFECT_SUPPORT

#ifndef MOJOSHADER_USE_SDL_STDLIB
#include <math.h>
#endif /* MOJOSHADER_USE_SDL_STDLIB */

void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
float *outregs)
Expand Down
62 changes: 62 additions & 0 deletions mojoshader_internal.h
Expand Up @@ -8,11 +8,71 @@
// Shader bytecode format is described at MSDN:
// http://msdn.microsoft.com/en-us/library/ff569705.aspx

#ifdef MOJOSHADER_USE_SDL_STDLIB
#include <SDL_assert.h>
#include <SDL_stdinc.h>

/* stdint.h */
typedef Uint8 uint8;
typedef Uint16 uint16;
typedef Uint32 uint32;
typedef Sint32 int32;
typedef Sint64 int64;
typedef Uint64 uint64;

/* assert.h */
#define assert SDL_assert

/* stdlib.h */
#define malloc SDL_malloc
#define free SDL_free

/* stdio.h */
#define sscanf SDL_sscanf
#define snprintf SDL_snprintf
#define vsnprintf SDL_vsnprintf

/* math.h */
#define acos SDL_acos
#define asin SDL_asin
#define atan SDL_atan
#define atan2 SDL_atan2
#define cos SDL_acos
#define exp SDL_exp
#define floor SDL_floor
#define log SDL_log
#define sin SDL_sin
#define sqrt SDL_sqrt

/* string.h */
#define strchr SDL_strchr
#define strcmp SDL_strcmp
/* TODO: Move MojoShader away from strcpy! This len is awful! */
#define strcpy(dst, src) SDL_strlcpy(dst, src, SDL_strlen(src) + 1)
#define strlen SDL_strlen
#define strncmp SDL_strncmp
#define strstr SDL_strstr

/* string.h but with undefs for Apple systems */
#ifdef memcmp
#undef memcmp
#endif
#define memcmp SDL_memcmp
#ifdef memcpy
#undef memcpy
#endif
#define memcpy SDL_memcpy
#ifdef memset
#define memset
#endif
#define memset SDL_memset
#else /* MOJOSHADER_USE_SDL_STDLIB */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#endif /* MOJOSHADER_USE_SDL_STDLIB */

#include "mojoshader.h"

Expand Down Expand Up @@ -114,6 +174,7 @@ typedef unsigned int uint; // this is a printf() helper. don't use for code.
// Locale-independent float printing replacement for snprintf
size_t MOJOSHADER_printFloat(char *text, size_t maxlen, float arg);

#ifndef MOJOSHADER_USE_SDL_STDLIB
#ifdef _MSC_VER
#include <float.h>
#include <malloc.h>
Expand Down Expand Up @@ -155,6 +216,7 @@ typedef uint64_t uint64;
#ifdef sun
#include <alloca.h>
#endif
#endif /* MOJOSHADER_USE_SDL_STDLIB */

#ifdef __GNUC__
#define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
Expand Down
6 changes: 0 additions & 6 deletions mojoshader_opengl.c
Expand Up @@ -7,12 +7,6 @@
* This file written by Ryan C. Gordon.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h> // GL headers need this for WINGDIAPI definition.
Expand Down

0 comments on commit 9a5fdb1

Please sign in to comment.