Ver Fonte

make sure all libs compile as C++

Sean Barrett há 10 anos atrás
pai
commit
3f17b24d90
5 ficheiros alterados com 112 adições e 6 exclusões
  1. 2 2
      stb_leakcheck.h
  2. 3 2
      stretchy_buffer.h
  3. 4 0
      tests/stb.dsp
  4. 0 1
      tests/stb_cpp.cpp
  5. 103 1
      tests/test_cpp_compilation.cpp

+ 2 - 2
stb_leakcheck.h

@@ -1,4 +1,4 @@
-// stb_leakcheck.h - v0.1 - quick & dirty malloc leak-checking - public domain
+// stb_leakcheck.h - v0.2 - quick & dirty malloc leak-checking - public domain
 
 #ifdef STB_LEAKCHECK_IMPLEMENTATION
 #undef STB_LEAKCHECK_IMPLEMENTATION // don't implenment more than once
@@ -27,7 +27,7 @@ static stb_leakcheck_malloc_info *mi_head;
 
 void *stb_leakcheck_malloc(size_t sz, char *file, int line)
 {
-   stb_leakcheck_malloc_info *mi = malloc(sz + sizeof(*mi));
+   stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));
    if (mi == NULL) return mi;
    mi->file = file;
    mi->line = line;

+ 3 - 2
stretchy_buffer.h

@@ -1,7 +1,8 @@
-// stretchy_buffer.h - v1.01 - public domain - nothings.org/stb
+// stretchy_buffer.h - v1.02 - public domain - nothings.org/stb
 // a vector<>-like dynamic array for C
 //
 // version history:
+//      1.02 -  compiles as C++, but untested
 //      1.01 -  added a "common uses" documentation section
 //      1.0  -  fixed bug in the version I posted prematurely
 //      0.9  -  rewrite to try to avoid strict-aliasing optimization
@@ -193,7 +194,7 @@ static void * stb__sbgrowf(void *arr, int increment, int itemsize)
    int dbl_cur = arr ? 2*stb__sbm(arr) : 0;
    int min_needed = stb_sb_count(arr) + increment;
    int m = dbl_cur > min_needed ? dbl_cur : min_needed;
-   int *p = realloc(arr ? stb__sbraw(arr) : 0, itemsize * m + sizeof(int)*2);
+   int *p = (int *) realloc(arr ? stb__sbraw(arr) : 0, itemsize * m + sizeof(int)*2);
    if (p) {
       if (!arr)
          p[1] = 0;

+ 4 - 0
tests/stb.dsp

@@ -150,6 +150,10 @@ SOURCE=..\stb_vorbis.c
 # End Source File
 # Begin Source File
 
+SOURCE=..\stb_voxel_render.h
+# End Source File
+# Begin Source File
+
 SOURCE=..\stretchy_buffer.h
 # End Source File
 # Begin Source File

+ 0 - 1
tests/stb_cpp.cpp

@@ -80,4 +80,3 @@ int main(int argc, char **argv)
    }
    return 0;
 }
-                     

+ 103 - 1
tests/test_cpp_compilation.cpp

@@ -8,7 +8,6 @@
 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
 #define STB_RECT_PACK_IMPLEMENTATION
 #define STB_VOXEL_RENDER_IMPLEMENTATION
-#define STBVOX_CONFIG_MODE 1
 
 #define STBI_MALLOC     my_malloc
 #define STBI_FREE       my_free
@@ -27,9 +26,112 @@ void my_free(void *) { }
 #include "stb_c_lexer.h"
 #include "stb_divide.h"
 #include "stb_herringbone_wang_tile.h"
+
+#define STBVOX_CONFIG_MODE 1
 #include "stb_voxel_render.h"
 
 #define STBTE_DRAW_RECT(x0,y0,x1,y1,color)      do ; while(0)
 #define STBTE_DRAW_TILE(x,y,id,highlight,data)  do ; while(0)
 #define STB_TILEMAP_EDITOR_IMPLEMENTATION
 #include "stb_tilemap_editor.h"
+
+#include "stb_easy_font.h"
+
+#define STB_LEAKCHECK_IMPLEMENTATION
+#include "stb_leakcheck.h"
+
+#define STB_IMAGE_RESIZE_IMPLEMENTATION
+#include "stb_image_resize.h"
+
+#include "stretchy_buffer.h"
+
+
+
+////////////////////////////////////////////////////////////
+//
+// text edit
+
+#include <stdlib.h>
+#include <string.h> // memmove
+#include <ctype.h>  // isspace
+
+#define STB_TEXTEDIT_CHARTYPE   char
+#define STB_TEXTEDIT_STRING     text_control
+
+// get the base type
+#include "stb_textedit.h"
+
+// define our editor structure
+typedef struct
+{
+   char *string;
+   int stringlen;
+   STB_TexteditState state;
+} text_control;
+
+// define the functions we need
+void layout_func(StbTexteditRow *row, STB_TEXTEDIT_STRING *str, int start_i)
+{
+   int remaining_chars = str->stringlen - start_i;
+   row->num_chars = remaining_chars > 20 ? 20 : remaining_chars; // should do real word wrap here
+   row->x0 = 0;
+   row->x1 = 20; // need to account for actual size of characters
+   row->baseline_y_delta = 1.25;
+   row->ymin = -1;
+   row->ymax =  0;
+}
+
+int delete_chars(STB_TEXTEDIT_STRING *str, int pos, int num)
+{
+   memmove(&str->string[pos], &str->string[pos+num], str->stringlen - (pos+num));
+   str->stringlen -= num;
+   return 1; // always succeeds
+}
+
+int insert_chars(STB_TEXTEDIT_STRING *str, int pos, STB_TEXTEDIT_CHARTYPE *newtext, int num)
+{
+   str->string = (char *) realloc(str->string, str->stringlen + num);
+   memmove(&str->string[pos+num], &str->string[pos], str->stringlen - pos);
+   memcpy(&str->string[pos], newtext, num);
+   str->stringlen += num;
+   return 1; // always succeeds
+}
+
+// define all the #defines needed 
+
+#define KEYDOWN_BIT                    0x80000000
+
+#define STB_TEXTEDIT_STRINGLEN(tc)     ((tc)->stringlen)
+#define STB_TEXTEDIT_LAYOUTROW         layout_func
+#define STB_TEXTEDIT_GETWIDTH(tc,n,i)  (1) // quick hack for monospaced
+#define STB_TEXTEDIT_KEYTOTEXT(key)    (((key) & KEYDOWN_BIT) ? 0 : (key))
+#define STB_TEXTEDIT_GETCHAR(tc,i)     ((tc)->string[i])
+#define STB_TEXTEDIT_NEWLINE           '\n'
+#define STB_TEXTEDIT_IS_SPACE(ch)      isspace(ch)
+#define STB_TEXTEDIT_DELETECHARS       delete_chars
+#define STB_TEXTEDIT_INSERTCHARS       insert_chars
+
+#define STB_TEXTEDIT_K_SHIFT           0x40000000
+#define STB_TEXTEDIT_K_CONTROL         0x20000000
+#define STB_TEXTEDIT_K_LEFT            (KEYDOWN_BIT | 1) // actually use VK_LEFT, SDLK_LEFT, etc
+#define STB_TEXTEDIT_K_RIGHT           (KEYDOWN_BIT | 2) // VK_RIGHT
+#define STB_TEXTEDIT_K_UP              (KEYDOWN_BIT | 3) // VK_UP
+#define STB_TEXTEDIT_K_DOWN            (KEYDOWN_BIT | 4) // VK_DOWN
+#define STB_TEXTEDIT_K_LINESTART       (KEYDOWN_BIT | 5) // VK_HOME
+#define STB_TEXTEDIT_K_LINEEND         (KEYDOWN_BIT | 6) // VK_END
+#define STB_TEXTEDIT_K_TEXTSTART       (STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_CONTROL)
+#define STB_TEXTEDIT_K_TEXTEND         (STB_TEXTEDIT_K_LINEEND   | STB_TEXTEDIT_K_CONTROL)
+#define STB_TEXTEDIT_K_DELETE          (KEYDOWN_BIT | 7) // VK_DELETE
+#define STB_TEXTEDIT_K_BACKSPACE       (KEYDOWN_BIT | 8) // VK_BACKSPACE
+#define STB_TEXTEDIT_K_UNDO            (KEYDOWN_BIT | STB_TEXTEDIT_K_CONTROL | 'z')
+#define STB_TEXTEDIT_K_REDO            (KEYDOWN_BIT | STB_TEXTEDIT_K_CONTROL | 'y')
+#define STB_TEXTEDIT_K_INSERT          (KEYDOWN_BIT | 9) // VK_INSERT
+#define STB_TEXTEDIT_K_WORDLEFT        (STB_TEXTEDIT_K_LEFT  | STB_TEXTEDIT_K_CONTROL)
+#define STB_TEXTEDIT_K_WORDRIGHT       (STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_CONTROL)
+#define STB_TEXTEDIT_K_PGUP            (KEYDOWN_BIT | 10) // VK_PGUP -- not implemented
+#define STB_TEXTEDIT_K_PGDOWN          (KEYDOWN_BIT | 11) // VK_PGDOWN -- not implemented
+
+#define STB_TEXTEDIT_IMPLEMENTATION
+#include "stb_textedit.h"
+
+