tilemap_editor_integration_example.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // This isn't compilable as-is, as it was extracted from a working
  2. // integration-in-a-game and makes reference to symbols from that game.
  3. #include <assert.h>
  4. #include <ctype.h>
  5. #include "game.h"
  6. #include "SDL.h"
  7. #include "stb_tilemap_editor.h"
  8. extern void editor_draw_tile(int x, int y, unsigned short tile, int mode, float *props);
  9. extern void editor_draw_rect(int x0, int y0, int x1, int y1, unsigned char r, unsigned char g, unsigned char b);
  10. static int is_platform(short *tiles);
  11. static unsigned int prop_type(int n, short *tiles);
  12. static char *prop_name(int n, short *tiles);
  13. static float prop_range(int n, short *tiles, int is_max);
  14. static int allow_link(short *src, short *dest);
  15. #define STBTE_MAX_PROPERTIES 8
  16. #define STBTE_PROP_TYPE(n, tiledata, p) prop_type(n,tiledata)
  17. #define STBTE_PROP_NAME(n, tiledata, p) prop_name(n,tiledata)
  18. #define STBTE_PROP_MIN(n, tiledata, p) prop_range(n,tiledata,0)
  19. #define STBTE_PROP_MAX(n, tiledata, p) prop_range(n,tiledata,1)
  20. #define STBTE_PROP_FLOAT_SCALE(n,td,p) (0.1)
  21. #define STBTE_ALLOW_LINK(srctile, srcprop, desttile, destprop) \
  22. allow_link(srctile, desttile)
  23. #define STBTE_LINK_COLOR(srctile, srcprop, desttile, destprop) \
  24. (is_platform(srctile) ? 0xff80ff : 0x808040)
  25. #define STBTE_DRAW_RECT(x0,y0,x1,y1,c) \
  26. editor_draw_rect(x0,y0,x1,y1,(c)>>16,((c)>>8)&255,(c)&255)
  27. #define STBTE_DRAW_TILE(x,y,id,highlight,props) \
  28. editor_draw_tile(x,y,id,highlight,props)
  29. #define STB_TILEMAP_EDITOR_IMPLEMENTATION
  30. #include "stb_tilemap_editor.h"
  31. stbte_tilemap *edit_map;
  32. void editor_key(enum stbte_action act)
  33. {
  34. stbte_action(edit_map, act);
  35. }
  36. void editor_process_sdl_event(SDL_Event *e)
  37. {
  38. switch (e->type) {
  39. case SDL_MOUSEMOTION:
  40. case SDL_MOUSEBUTTONDOWN:
  41. case SDL_MOUSEBUTTONUP:
  42. case SDL_MOUSEWHEEL:
  43. stbte_mouse_sdl(edit_map, e, 1.0f/editor_scale,1.0f/editor_scale,0,0);
  44. break;
  45. case SDL_KEYDOWN:
  46. if (in_editor) {
  47. switch (e->key.keysym.sym) {
  48. case SDLK_RIGHT: editor_key(STBTE_scroll_right); break;
  49. case SDLK_LEFT : editor_key(STBTE_scroll_left ); break;
  50. case SDLK_UP : editor_key(STBTE_scroll_up ); break;
  51. case SDLK_DOWN : editor_key(STBTE_scroll_down ); break;
  52. }
  53. switch (e->key.keysym.scancode) {
  54. case SDL_SCANCODE_S: editor_key(STBTE_tool_select); break;
  55. case SDL_SCANCODE_B: editor_key(STBTE_tool_brush ); break;
  56. case SDL_SCANCODE_E: editor_key(STBTE_tool_erase ); break;
  57. case SDL_SCANCODE_R: editor_key(STBTE_tool_rectangle ); break;
  58. case SDL_SCANCODE_I: editor_key(STBTE_tool_eyedropper); break;
  59. case SDL_SCANCODE_L: editor_key(STBTE_tool_link); break;
  60. case SDL_SCANCODE_G: editor_key(STBTE_act_toggle_grid); break;
  61. }
  62. if ((e->key.keysym.mod & KMOD_CTRL) && !(e->key.keysym.mod & ~KMOD_CTRL)) {
  63. switch (e->key.keysym.scancode) {
  64. case SDL_SCANCODE_X: editor_key(STBTE_act_cut ); break;
  65. case SDL_SCANCODE_C: editor_key(STBTE_act_copy ); break;
  66. case SDL_SCANCODE_V: editor_key(STBTE_act_paste); break;
  67. case SDL_SCANCODE_Z: editor_key(STBTE_act_undo ); break;
  68. case SDL_SCANCODE_Y: editor_key(STBTE_act_redo ); break;
  69. }
  70. }
  71. }
  72. break;
  73. }
  74. }
  75. void editor_init(void)
  76. {
  77. int i;
  78. edit_map = stbte_create_map(20,14, 8, 16,16, 100);
  79. stbte_set_background_tile(edit_map, T_empty);
  80. for (i=0; i < T__num_types; ++i) {
  81. if (i != T_reserved1 && i != T_entry && i != T_doorframe)
  82. stbte_define_tile(edit_map, 0+i, 1, "Background");
  83. }
  84. stbte_define_tile(edit_map, 256+O_player , 8, "Char");
  85. stbte_define_tile(edit_map, 256+O_robot , 8, "Char");
  86. for (i=O_lockeddoor; i < O__num_types-2; ++i)
  87. if (i == O_platform || i == O_vplatform)
  88. stbte_define_tile(edit_map, 256+i, 4, "Object");
  89. else
  90. stbte_define_tile(edit_map, 256+i, 2, "Object");
  91. //stbte_set_layername(edit_map, 0, "background");
  92. //stbte_set_layername(edit_map, 1, "objects");
  93. //stbte_set_layername(edit_map, 2, "platforms");
  94. //stbte_set_layername(edit_map, 3, "characters");
  95. }
  96. static int is_platform(short *tiles)
  97. {
  98. // platforms are only on layer #2
  99. return tiles[2] == 256 + O_platform || tiles[2] == 256 + O_vplatform;
  100. }
  101. static int is_object(short *tiles)
  102. {
  103. return (tiles[1] >= 256 || tiles[2] >= 256 || tiles[3] >= 256);
  104. }
  105. static unsigned int prop_type(int n, short *tiles)
  106. {
  107. if (is_platform(tiles)) {
  108. static unsigned int platform_types[STBTE_MAX_PROPERTIES] = {
  109. STBTE_PROP_bool, // phantom
  110. STBTE_PROP_int, // x_adjust
  111. STBTE_PROP_int, // y_adjust
  112. STBTE_PROP_float, // width
  113. STBTE_PROP_float, // lspeed
  114. STBTE_PROP_float, // rspeed
  115. STBTE_PROP_bool, // autoreturn
  116. STBTE_PROP_bool, // one-shot
  117. // remainder get 0, means 'no property in this slot'
  118. };
  119. return platform_types[n];
  120. } else if (is_object(tiles)) {
  121. if (n == 0)
  122. return STBTE_PROP_bool;
  123. }
  124. return 0;
  125. }
  126. static char *prop_name(int n, short *tiles)
  127. {
  128. if (is_platform(tiles)) {
  129. static char *platform_vars[STBTE_MAX_PROPERTIES] = {
  130. "phantom",
  131. "x_adjust",
  132. "y_adjust",
  133. "width",
  134. "lspeed",
  135. "rspeed",
  136. "autoreturn",
  137. "one-shot",
  138. };
  139. return platform_vars[n];
  140. }
  141. return "phantom";
  142. }
  143. static float prop_range(int n, short *tiles, int is_max)
  144. {
  145. if (is_platform(tiles)) {
  146. static float ranges[8][2] = {
  147. { 0, 1 }, // phantom-flag, range is ignored
  148. { -15, 15 }, // x_adjust
  149. { -15, 15 }, // y_adjust
  150. { 0, 6 }, // width
  151. { 0, 10 }, // lspeed
  152. { 0, 10 }, // rspeed
  153. { 0, 1 }, // autoreturn, range is ignored
  154. { 0, 1 }, // one-shot, range is ignored
  155. };
  156. return ranges[n][is_max];
  157. }
  158. return 0;
  159. }
  160. static int allow_link(short *src, short *dest)
  161. {
  162. if (is_platform(src))
  163. return dest[1] == 256+O_lever;
  164. if (src[1] == 256+O_endpoint)
  165. return is_platform(dest);
  166. return 0;
  167. }