Commits

kasicass committed 9e65688

resid module, done

  • Participants
  • Parent commits b37ded4

Comments (0)

Files changed (9)

 SRCFILES = \
 	src/gui/canvas.c											\
 	src/gui/window.c											\
-	src/main.c
+	src/resource.c												\
+	src/util/resid.c											\
+	src/simpletest.c
 
 CFLAGS  = -g -Wall -Isrc -I/usr/local/include
 LDFLAGS = -L/usr/local/lib -lSDL2
 the C version
 
 ==== TODO ====
+ + util/resid.h/.c 
  + simple gui system v1 (6 tomatoes)
  + map editor v1
- + map render v1
+ + map rendering v1, simple JSON format rendering
 
 ==== DONE ====
 2012-04-15

File 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];
+}
+

File 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

File c/src/simpletest.c

+#include "util/resid.h"
+#include <stdio.h>
+
+void residTest()
+{
+	struct RString*rs1, *rs2;
+	RESID_init();
+
+	rs1 = RSTR_alloc("map/castle.json");
+	printf("rs1, len:%u, cstr:%s, rid:%u\n", RSTR_len(rs1),
+		RSTR_cstr(rs1), RSTR_toRESID(rs1));
+
+	rs2 = RSTR_alloc("map/tile.json");
+	printf("rs2, len:%u, cstr:%s, rid:%u\n", RSTR_len(rs2),
+		RSTR_cstr(rs2), RSTR_toRESID(rs2));
+
+	rs2 = RSTR_alloc("map/castle.json");
+	printf("rs1:%p, %u; rs2:%p, %u\n", rs1, RSTR_toRESID(rs1),
+		rs2, RSTR_toRESID(rs2));
+
+	printf("rs2:%u, %s\n", RSTR_toRESID(rs2), RESID_toCStr(RSTR_toRESID(rs2)));
+
+	RESID_shutdown();
+}
+
+int main()
+{
+	residTest();
+	return 0;
+}

File c/src/util/resid.c

+#include "resid.h"
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#define RESID_COUNT 8192
+
+struct RString
+{
+	char *cstr;
+	unsigned int len;
+	RESID rid;
+};
+
+struct RESBase
+{
+	struct RString slot[RESID_COUNT];
+	unsigned int index;
+};
+
+static struct RESBase g_base;
+
+void
+RESID_init()
+{
+}
+
+void
+RESID_shutdown()
+{
+	unsigned int i;
+	for (i = 0; i < g_base.index; i++)
+	{
+		free(g_base.slot[i].cstr);
+	}
+}
+
+static struct RString*
+RSTR_findFromBase(const char *s)
+{
+	unsigned int i;
+	for (i = 0; i < g_base.index; i++)
+	{
+		if (strcmp(s, g_base.slot[i].cstr) == 0)
+			return &g_base.slot[i];
+	}
+
+	return NULL;
+}
+
+struct RString *
+RSTR_alloc(const char *s)
+{
+	struct RString *rs;
+	rs = RSTR_findFromBase(s);
+	if (rs)
+		return rs;
+
+	assert(g_base.index <= RESID_COUNT);
+	rs = &g_base.slot[g_base.index];
+	rs->rid  = g_base.index;
+	g_base.index++;
+
+	rs->len  = strlen(s);
+	rs->cstr = (char*)malloc(rs->len+1);
+	strcpy(rs->cstr, s);
+	return rs;
+}
+
+unsigned int
+RSTR_len(struct RString *rs)
+{
+	return rs->len;
+}
+
+const char *
+RSTR_cstr(struct RString *rs)
+{
+	return rs->cstr;
+}
+
+RESID
+RSTR_toRESID(struct RString *rs)
+{
+	return rs->rid;
+}
+
+struct RString *
+RESID_toRString(RESID rid)
+{
+	assert(0 <= rid && rid < g_base.index);
+	return &g_base.slot[rid];
+}
+
+const char *
+RESID_toCStr(RESID rid)
+{
+	assert(0 <= rid && rid < g_base.index);
+	return g_base.slot[rid].cstr;
+}
+

File c/src/util/resid.h

+#ifndef THESWORD2_RESID_H
+#define THESWORD2_RESID_H
+
+struct RString;
+typedef unsigned int RESID;
+
+struct RString *RSTR_alloc(const char *s); // alloc only, no free
+unsigned int RSTR_len(struct RString *rs);
+const char *RSTR_cstr(struct RString *rs);
+RESID RSTR_toRESID(struct RString *rs);
+
+struct RString *RESID_toRString(RESID id);
+const char *RESID_toCStr(RESID id);
+
+void RESID_init();
+void RESID_shutdown();
+
+#endif

File labs/multi_renderer/Makefile

+FILENAME = test.c
+
+all:
+	gcc -I/usr/local/include $(FILENAME) -L/usr/local/lib -lSDL2

File labs/multi_renderer/test.c

+#include <SDL2/SDL.h>
+#include <stdlib.h>
+#include <assert.h>
+
+int main()
+{
+	int result;
+	SDL_Window *win = NULL;
+	SDL_Renderer *render1 = NULL;
+	SDL_Renderer *render2 = NULL;
+	SDL_Event event;
+
+	result = SDL_Init(SDL_INIT_VIDEO);
+	assert(result == 0);
+
+	win = SDL_CreateWindow("#1", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
+	assert(win != NULL);
+
+	render1 = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
+	assert(render1 != NULL);
+
+	render2 = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
+	assert(render2 != NULL);		// fail, no multi-renderer
+
+	
+	SDL_DestroyRenderer(render1);
+	SDL_DestroyRenderer(render2);
+	SDL_DestroyWindow(win);
+	SDL_Quit();
+	return 0;
+}
+