kasicass avatar kasicass committed ef38359

resource module refactor

Comments (0)

Files changed (11)

 the C version
 
 ==== TODO ====
- + util/resid.h/.c 
  + simple gui system v1 (6 tomatoes)
  + map editor v1
  + map rendering v1, simple JSON format rendering
+ + drawing fundermentals
 
 ==== DONE ====
+2012-04-20
+ + util/resid.h/.c (2 tomatoes)
+
 2012-04-15
  + JSON test (2 tomatoes)
  + map format v1 (1 tomatoes)
+#include "drawing.h"
+#include "util/klist.h"
+#include <assert.h>
+
+struct DrawItemInternal
+{
+	K_LIST_ENTRY(DrawItemInternal) entry;
+	DrawItem data;
+};
+
+struct DrawingData
+{
+	SDL_Renderer *renderer;
+	SDL_Surface *surface;
+
+	K_LIST_HEAD(, DrawItemInternal) drawList;
+	K_LIST_HEAD(, DrawItemInternal) freeList;
+};
+
+struct DrawingData g_draw;
+
+void D_init(SDL_Renderer *renderer, int w, int h)
+{
+	Uint32 rmask, gmask, bmask, amask;
+
+	assert(g_draw.surface == NULL);
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+	rmask = 0xff000000;
+	gmask = 0x00ff0000;
+	bmask = 0x0000ff00;
+	amask = 0;
+#else
+	rmask = 0x000000ff;
+	gmask = 0x0000ff00;
+	bmask = 0x00ff0000;
+	amask = 0;
+#endif
+	g_draw.surface = SDL_CreateRGBSurface(0, w, h, 24, rmask, gmask, bmask, amask);
+	assert(g_draw.surface != NULL);
+
+	g_draw.renderer = renderer;
+	K_LIST_INIT(&g_draw.freeList);
+	K_LIST_INIT(&g_draw.drawList);
+}
+
+void D_shutdown();
+{
+	
+}
+
+void D_beginDrawing()
+{
+	SDL_Surface *main = g_draw.surface;
+	Uint32 color = SDL_MapRGB(main->format, 0, 0, 0);
+	SDL_FillRect(main, NULL, color);
+}
+
+void D_addItem(enum DLAYER layer, struct DrawItem data)
+{
+	struct DrawItemInternal *item = K_LIST_FIRST(g_draw.freeList);
+	if (!item)
+	{
+		item = malloc(sizeof(*item));
+	}
+
+	item.data = data;
+	K_LIST_INSERT_HEAD(&g_draw.drawList, item, entry);
+}
+
+static void D_drawItem(struct DrawItem *item)
+{
+	SDL_Surface *main = g_draw.surface;
+	SDL_BlitSurface(item
+}
+
+void D_endDrawing()
+{
+	struct DrawItemInternal *item;
+	K_LIST_FOREACH(item, &g_draw.drawList, entry)
+	{
+		D_drawItem(&item->data);
+	}
+
+	g_draw.freeList = g_draw.drawList;
+}
+
+#ifndef THESWORD2_DRAWING_H
+#define THESWORD2_DRAWING_H
+
+#include <SDL2/SDL.h>
+
+enum DLAYER
+{
+	LAYER_MAP_1 = 0,
+	LAYER_MAP_2 = 1,
+};
+
+struct DrawItem
+{
+	RESID rid;
+	SDL_Rect srcRect;
+	SDL_Rect destRect;
+};
+
+void D_init(SDL_Renderer *renderer, int w, int h);
+void D_shutdown();
+
+void D_beginDrawing();
+void D_addItem(enum DLAYER layer, struct DrawItem item);
+void D_endDrawing();
+
+#endif

c/src/resource.c

-#include "resource.h"
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define RESBUF_SIZE   256
-#define RESBUF_COUNT  5
-
-struct RC_Data
-{
-	const char *basePath;
-	char resBuf[RESBUF_COUNT][RESBUF_SIZE];
-	int i;
-};
-
-struct RC_Data g_rcData;
-
-void RC_init(const char *basePath)
-{
-	assert(basePath != NULL);
-	assert(g_rcData.basePath == NULL);
-
-	g_rcData.basePath = basePath;
-}
-
-const char *RC_getResPath(const char *resName)
-{
-	int i = g_rcData.i;
-	g_rcData.i = (g_rcData.i + 1) % RESBUF_COUNT;
-	strcpy(g_rcData.resBuf[i], g_rcData.basePath);
-	strcat(g_rcData.resBuf[i], resName);
-	return g_rcData.resBuf[i];
-}
-

c/src/resource.h

-#ifndef THESWORD2_RESOURCE_H
-#define THESWORD2_RESOURCE_H
-
-void RC_init(const char *basePath);
-const char *RC_getResPath(const char *resName);
-
-
-#endif

c/src/resource/image.c

+#include "resource.h"
+#include "util/config.h"
+
+struct ImageBlob
+{
+	SDL_Surface *slot[RESID_COUNT];
+};
+
+struct ImageBlob g_blob;
+
+SDL_Surface* RC_getSurface(RESID rid)
+{
+	if (!g_blob.slot[rid])
+	{
+		const char *path = RC_getResPath(RESID_toCStr(rid));
+		g_blob.slot[rid] = SDL_LoadBMP(path);
+		assert(g_blob.slot[rid] != NULL);
+	}
+
+	return g_blob.slot[rid];
+}
+

c/src/resource/path.c

+#include "resource.h"
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define RESBUF_SIZE   256
+#define RESBUF_COUNT  5
+
+struct RC_Data
+{
+	const char *basePath;
+	char resBuf[RESBUF_COUNT][RESBUF_SIZE];
+	int i;
+};
+
+struct RC_Data g_rcData;
+
+void RC_init(const char *basePath)
+{
+	assert(basePath != NULL);
+	assert(g_rcData.basePath == NULL);
+
+	g_rcData.basePath = basePath;
+}
+
+const char *RC_getResPath(const char *resName)
+{
+	int i = g_rcData.i;
+	g_rcData.i = (g_rcData.i + 1) % RESBUF_COUNT;
+	strcpy(g_rcData.resBuf[i], g_rcData.basePath);
+	strcat(g_rcData.resBuf[i], resName);
+	return g_rcData.resBuf[i];
+}
+

c/src/resource/resource.h

+#ifndef THESWORD2_RESOURCE_H
+#define THESWORD2_RESOURCE_H
+
+#include "util/resid.h"
+#include <SDL2/SDL.h>
+
+void RC_init(const char *basePath);
+const char *RC_getResPath(const char *resName);
+
+SDL_Surface* RC_getSurface(RESID rid);
+
+#endif

c/src/util/config.h

+#ifndef THESWORD2_UTIL_CONFIG_H
+#define THESWORD2_UTIL_CONFIG_H
+
+#define RESID_COUNT 8192
+
+#endif

c/src/util/klist.h

+#ifndef KCODE_COMMON_LIST_H
+#define KCODE_COMMON_LIST_H
+
+
+// ------------- double-linked list ---------------
+
+/*
+ * List declarations.
+ */
+#define	K_LIST_HEAD(name, type)                     \
+struct name {                                       \
+	struct type *lh_first;	/* first element */     \
+}
+
+#define	K_LIST_HEAD_INITIALIZER 			{ NULL }
+
+#define	K_LIST_ENTRY(type)                          \
+struct {                                            \
+	struct type *le_next;	/* next element */      \
+	struct type **le_prev;	/* address of previous next element */	\
+}
+
+/*
+ * List functions.
+ */
+#define	K_LIST_EMPTY(head)	((head)->lh_first == NULL)
+
+#define	K_LIST_FIRST(head)	((head)->lh_first)
+
+#define	K_LIST_FOREACH(var, head, field)            \
+    for ((var) = K_LIST_FIRST((head));              \
+        (var);                                      \
+        (var) = K_LIST_NEXT((var), field))
+
+#define	K_LIST_FOREACH_SAFE(var, head, field, tvar)         \
+	for ((var) = K_LIST_FIRST((head));                      \
+	    (var) && ((tvar) = K_LIST_NEXT((var), field), 1);   \
+	    (var) = (tvar))
+
+#define	K_LIST_INIT(head) do {                      \
+    K_LIST_FIRST((head)) = NULL;                    \
+} while (0)
+
+#define	K_LIST_INSERT_AFTER(listelm, elm, field) do {                         \
+    if ((K_LIST_NEXT((elm), field) = K_LIST_NEXT((listelm), field)) != NULL)  \
+        K_LIST_NEXT((listelm), field)->field.le_prev =                        \
+            &K_LIST_NEXT((elm), field);                                       \
+    K_LIST_NEXT((listelm), field) = (elm);                                    \
+    (elm)->field.le_prev = &K_LIST_NEXT((listelm), field);                    \
+} while (0)
+
+#define	K_LIST_INSERT_BEFORE(listelm, elm, field) do {                        \
+    (elm)->field.le_prev = (listelm)->field.le_prev;                          \
+    K_LIST_NEXT((elm), field) = (listelm);                                    \
+    *(listelm)->field.le_prev = (elm);                                        \
+    (listelm)->field.le_prev = &K_LIST_NEXT((elm), field);                    \
+} while (0)
+
+#define	K_LIST_INSERT_HEAD(head, elm, field) do {                             \
+    if ((K_LIST_NEXT((elm), field) = K_LIST_FIRST((head))) != NULL)           \
+        K_LIST_FIRST((head))->field.le_prev = &K_LIST_NEXT((elm), field);     \
+        K_LIST_FIRST((head)) = (elm);                                         \
+    (elm)->field.le_prev = &K_LIST_FIRST((head));                             \
+} while (0)
+
+#define	K_LIST_NEXT(elm, field)	((elm)->field.le_next)
+
+#define	K_LIST_REMOVE(elm, field) do {                      \
+    if (K_LIST_NEXT((elm), field) != NULL)                  \
+        K_LIST_NEXT((elm), field)->field.le_prev =          \
+            (elm)->field.le_prev;                           \
+    *(elm)->field.le_prev = K_LIST_NEXT((elm), field);      \
+} while (0)
+
+#endif

c/src/util/resid.c

 #include "resid.h"
+#include "config.h"
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 
-#define RESID_COUNT 8192
-
 struct RString
 {
 	char *cstr;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.