static char getPrintableChar(Entity *mapCases, Entity *res) { size_t ret = ywMapGetIdByElem(yeGet(mapCases, yeLen(mapCases) - 1)); res = yeGet(yeGet(res, ret), "map-char"); return res != NULL ? yeGetString(res)[0] : '-'; }
Entity *ywMapGetCase(YWidgetState *state, Entity *pos) { Entity *map = yeGet(state->entity, "map"); int w = yeGetInt(yeGet(state->entity, "width")); return yeGet(map, yeGetInt(yeGet(pos, "x")) + (w * yeGetInt(yeGet(pos, "y")))); }
static SDL_Texture *sdlLoasAndCachImg(Entity *elem) { const char *path; SDL_Texture *texture = yeGetData(yeGet(elem, "$sdl-img")); Entity *data; if (texture) return texture; SDL_Surface *image; path = yeGetString(yeGet(elem, "map-srite")); if (!path) { return NULL; } image = IMG_Load(path); if (!image) { return NULL; } texture = SDL_CreateTextureFromSurface(sg.renderer, image); data = yeCreateData(texture, elem, "$sdl-img"); yeSetDestroy(data, sdlFreeTexture); SDL_FreeSurface(image); return texture; }
void sdlResize(YWidgetState *wid, int renderType) { SDLWid *swid = wid->renderStates[renderType].opac; Entity *pos = yeGet(wid->entity, "pos"); swid->rect.h = yeGetInt(yeGet(pos, "h")) * WIN_H_SIZE / 1000; swid->rect.w = yeGetInt(yeGet(pos, "w")) * WIN_W_SIZE / 1000; swid->rect.x = yeGetInt(yeGet(pos, "x")) * WIN_W_SIZE / 1000; swid->rect.y = yeGetInt(yeGet(pos, "y")) * WIN_H_SIZE / 1000; }
void resize(YWidgetState *wid, int renderType) { CWidget *state = wid->renderStates[renderType].opac; Entity *pos = yeGet(wid->entity, "wid-pos"); state->h = yeGetInt(yeGet(pos, "h")) * LINES / 1000; state->w = yeGetInt(yeGet(pos, "w")) * COLS / 1000; state->x = yeGetInt(yeGet(pos, "x")) * COLS / 1000; state->y = yeGetInt(yeGet(pos, "y")) * LINES / 1000; wresize(state->win, state->h, state->w); mvwin(state->win, state->y, state->x); wborder(state->win, '|', '|', '-','-','+','+','+','+'); refresh(); }
void testVerticalContenerSdl(void) { GameConfig cfg; Entity *ret; YWidgetState *wid; int t = ydJsonInit(); void *jsonManager; /* Init libs */ g_assert(!ygInitGameConfig(&cfg, NULL, SDL2)); g_assert(!ygInit(&cfg)); /* Parsing json */ jsonManager = ydNewManager(t); ret = ydFromFile(jsonManager, TESTS_PATH"/widget.json"); ret = yeGet(ret, "ContenerTest"); g_assert(ret); ywinAddCallback(ywinCreateNativeCallback("menuTest", testMenuEnter)); wid = ywidNewWidget(ret, NULL); g_assert(wid); do { g_assert(ywidRend(wid) != -1); } while(ywidDoTurn(wid) != ACTION); YE_DESTROY(ret); ygEnd(); }
int ywMapGetIdByElem(Entity *mapElem) { if (yeType(mapElem) == YINT) return yeGetInt(mapElem); if (yeType(mapElem) == YARRAY) return yeGetInt(yeGet(mapElem, "id")); return -1; }
void testLifeDeathRebirdAndAgain(void) { Entity *mainEnt = yeCreateArray(NULL, NULL); Entity *map; Entity *tmp; yeCreateInt(100, mainEnt, "width"); map = yeCreateArray(mainEnt, "map"); g_assert(map); for (int i = 0; i < 2000; ++i) { tmp = yeCreateArray(map, NULL); g_assert(tmp); g_assert(yeCreateInt(0, tmp, NULL)); } tmp = yeGet(map, 500); g_assert(tmp); yeCreateInt(1, tmp, "hr"); int good = 0;; for (int j = 500; j < 1000; ++j) { tmp = yeGet(map, j); for (unsigned int i = 0; i < yeLen(tmp); ++i) { Entity *curHero = yeGet(tmp, i); if (yeGetInt(curHero) == 1) { good = 1; /* You can get it Noww !!!! */ g_assert(yeLen(tmp) == 2); yeRemoveChild(tmp, curHero); g_assert(yeLen(tmp) == 1); break; } } } g_assert(good); YE_DESTROY(mainEnt); }
void testEntityPatch(void) { yeInitMem(); int t = ydJsonInit(); void *jsonManager; Entity *ret; Entity *mn; Entity *mn2; Entity *entries; Entity *patch; jsonManager = ydNewManager(t); ret = ydFromFile(jsonManager, TESTS_PATH"/widget.json", NULL); mn = yeGet(ret, "MenuTest"); mn2 = yeCreateArray(NULL, NULL); yeCopy(mn, mn2); entries = yeGet(mn2, "entries"); yeRemoveChild(entries, 2); g_assert(yeLen(entries) == 2); patch = yePatchCreate(mn, mn2, NULL, NULL); g_assert(patch); yePatchAply(mn, patch); yeDestroy(patch); g_assert(yeLen(yeGet(mn, "entries")) == 2); /* Manu rentre chez toi c'est a moi qu'tu fait d'la peine... */ yeReCreateString("manu", mn2, "<type>"); yeCreateString("ayoyoyo", mn2, "wololo"); yeRemoveChild(mn2, "background"); yeReCreateInt(1337, mn2, "button_background"); yeSetAt(yeGet(entries, 0), "text", "new text"); yeCreateFloat(3.14, entries, NULL); patch = yePatchCreate(mn, mn2, NULL, NULL); g_assert(patch); yePatchAply(mn, patch); g_assert(!yeStrCmpAt(mn, "ayoyoyo", "wololo")); /* une gonsesse de perdu c'est 10 copains qui reviennes */ g_assert(!yeStrCmpAt(mn, "manu", "<type>")); g_assert(yeGetIntAt(mn, "button_background") == 1337); g_assert(!yeGet(mn, "background")); entries = yeGet(mn, "entries"); g_assert(yeGetFloatAt(entries, 2) == 3.14); g_assert(!yeStrCmpAt(yeGet(entries, 0), "new text", "text")); ydJsonEnd(); ydDestroyManager(jsonManager); yeEnd(); }
static int mapInit(YWidgetState *opac, Entity *entity, void *args) { const char *action; Entity *initer = yeGet(entity, "init"); ywidGenericInit(opac, t); ((YMapState *)opac)->resources = yeGet(entity, "resources"); ((YMapState *)opac)->pos = ywMapCreatePos(0, 0, NULL, NULL); ((YMapState *)opac)->actionIdx = ywidAddSignal(opac, "action"); action = yeGetString(yeGet(entity, "action")); ywidBind(opac, "action", action); if (initer) { YCallback *callback = ywinGetCallbackByStr(yeGetString(initer)); if (callback) ywidCallCallback(callback, opac, NULL, entity); } (void)args; return 0; }
static int cursesRender(YWidgetState *state, int t) { CWidget *wid = ywidGetRenderData(state, t); int x,y,h,w; unsigned int curx = 0, cury = 0; Entity *map = yeGet(state->entity, "map"); Entity *res = ywMapGetResources(state); unsigned int lenMap = yeLen(map); unsigned int wMap = yeGetInt(yeGet(state->entity, "width")); unsigned int hMap = lenMap / wMap; wmove(wid->win,0,0); getyx(wid->win, y, x); getmaxyx(wid->win, h, w); w -= x; h -= y; for (unsigned int i = 0; i < lenMap; ++i) { Entity *mapCase = yeGet(map, i); if (curx >= wMap) { curx = 0; ++cury; } mvwaddch(wid->win, (cury + 1) + (h/2 - hMap / 2), (curx + 1) + (w/2 - wMap / 2), getPrintableChar(mapCase, res)); ++curx; } wrefresh(wid->win); return 0; }
void testPanelMenuSdl2(void) { yeInitMem(); int t = ydJsonInit(); void *jsonManager; Entity *oret, *ret; YWidgetState *wid; ysRegistreFunc(ysNativeManager(), "menuTest", testMenuEnter); /* load files */ g_assert(t != -1); g_assert(ydJsonGetType() == t); jsonManager = ydNewManager(t); g_assert(jsonManager != NULL); oret = ydFromFile(jsonManager, TESTS_PATH"/widget.json", NULL); ret = yeGet(oret, "MenuTest"); g_assert(ret); g_assert(!ydJsonEnd()); g_assert(!ydDestroyManager(jsonManager)); t = ywMenuInit(); g_assert(t == 0); g_assert(ysdl2Init() != -1); g_assert(ysdl2Type() == 0); yeCreateString("panel", ret, "mn-type"); g_assert(ysdl2RegistreMenu() == 0); wid = ywidNewWidget(ret, NULL); g_assert(wid); do { g_assert(ywidRend(wid) != -1); } while(ywidDoTurn(wid) != ACTION); g_assert(!ywMenuEnd()); YWidDestroy(wid); ysdl2Destroy(); /* end libs */ YE_DESTROY(oret); yeEnd(); }
int sdlDisplaySprites(SDLWid *wid, int x, int y, Entity *elem, int w, int h, int thresholdX) { SDL_Color color = {0,0,0,255}; SDL_Rect DestR; SDL_Texture *texture = sdlLoasAndCachImg(elem); if (texture) { DestR.x = x * w + wid->rect.x + thresholdX; DestR.y = y * h + wid->rect.y; DestR.w = w; DestR.h = h; SDL_RenderCopy(sg.renderer, texture, NULL, &DestR); } else { return sdlPrintText(wid, yeGetString(yeGet(elem, "map-char")), 2, color, x * w, y * h); } return 0; }
int main(void) { yeInitMem(); GameConfig cfg; char buff[1024]; Entity *modDesc = yeCreateArray(NULL, NULL); Entity *tmp; Entity *map; Entity *path; yuiDebugInit(); //Can not be init twice :) TRY_OR_DIE(ygInitGameConfig(&cfg, gamePath, NONE), -1); TRY_OR_DIE(ygInit(&cfg), die(-1, &cfg)); /* put current path inside buff */ getcwd(buff, 1024); yuistrcpy(buff + yuistrlen(buff), "/test.sm"); path = yeCreateString(buff, NULL, NULL); tmp = yeCreateArray(modDesc, NULL); yeCreateString(".", tmp, "map-char"); tmp = yeCreateArray(modDesc, NULL); yeCreateString("#",tmp, "map-char"); tmp = yeCreateArray(modDesc, NULL); yeCreateString("_", tmp, "map-char"); ygLoadMod("../../../modules/sm-reader/"); map = yesCall(yeGet(ygGetMod("sm-reader"), "load-map"), path, modDesc); char *tmpStr = yeToCStr(map, 4, 0); printf("map: %s\n", tmpStr); g_free(tmpStr); tmpStr = yeToCStr(modDesc, 3, 0); printf("m-d: %s\n", tmpStr); g_free(tmpStr); YE_DESTROY(path); YE_DESTROY(map); YE_DESTROY(modDesc); return die(0, &cfg); }
static int ygParseStartAndGame(GameConfig *config, Entity *mainMod) { Entity *type = yeGet(mainMod, "type"); Entity *file = yeGet(mainMod, "file"); Entity *starting_widget = yeGet(mainMod, "starting widget"); Entity *preLoad = yeGet(mainMod, "pre-load"); Entity *initScripts = yeGet(mainMod, "init-scripts"); YWidgetState *wid; alive = 1; YE_ARRAY_FOREACH(preLoad, var) { Entity *tmpType = yeGet(var, "type"); Entity *tmpFile = yeGet(var, "file"); if (yuiStrEqual0(yeGetString(tmpType), "lua")) { if (ysLoadFile(luaManager, yeGetString(tmpFile)) < 0) { DPRINT_ERR("Error when loading '%s': %s\n", yeGetString(tmpFile), ysGetError(luaManager)); } } }
void testListMod(void) { yeInitMem(); GameConfig cfg; Entity *gc = yeCreateArray(NULL, NULL); Entity *e1 = yeCreateInt(0, gc, NULL); Entity *e2 = yeCreateInt(0, gc, NULL); Entity *e3 = yeCreateInt(0, gc, NULL); Entity *l; Entity *l2; Entity *l3; g_assert(!ygInitGameConfig(&cfg, NULL, NONE)); g_assert(!ygInit(&cfg)); ygLoadMod(TESTS_PATH"../modules/list/"); #define list_init_from_array(elem, father, name) ysCall(ygGetTccManager(), \ "list_init_from_array", \ elem, father, name) #define list_elem(list) ysCall(ygGetTccManager(), "list_elem", list) #define list_insert(list, elem) ysCall(ygGetTccManager(), "list_insert", \ list, elem) #define list_next(list) ysCall(ygGetTccManager(), "list_next", list) #define list_prev(list) ysCall(ygGetTccManager(), "list_prev", list) #define list_head(list) ysCall(ygGetTccManager(), "list_head", list) #define list_last(list) ysCall(ygGetTccManager(), "list_last", list) #define list_pop(list) ysCall(ygGetTccManager(), "list_pop", list) #define list_insert_before(list, elem) ysCall(ygGetTccManager(), \ "list_insert_before", \ list, elem) #define list_roll(list) ysCall(ygGetTccManager(), "list_roll", list) #define list_back_roll(list) ysCall(ygGetTccManager(), "list_back_roll", list) #define list_to_array(list, father, name) ysCall(ygGetTccManager(),\ "list_to_array", \ list, father, name) l = ysCall(ygGetTccManager(), "list_init", e1); g_assert(l); g_assert(list_elem(l) == e1); g_assert(list_prev(l) == l); g_assert(list_next(l) == l); /* test insert */ g_assert(list_insert(l, e2) == l); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e2); g_assert(list_elem(list_next(list_next(l))) == e1); g_assert(list_elem(list_next(list_prev(l))) == e1); /* pop second elem */ l2 = list_pop(list_next(l)); g_assert(l2 == l); g_assert(list_elem(l) == e1); g_assert(list_prev(l) == l); g_assert(list_next(l) == l); /* insert l2 before l1 */ l2 = list_insert_before(l, e2); g_assert(l2 != l); g_assert(list_elem(l) == e1); g_assert(list_elem(l2) == e2); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e2); g_assert(list_elem(list_next(list_next(l))) == e1); g_assert(list_elem(list_next(list_prev(l))) == e1); /* insert l3 before l1 */ /* l2 -> l3 -> l */ l3 = list_insert_before(l, e3); g_assert(l3 == l2); l3 = list_next(l2); g_assert(list_head(l) == l2); g_assert(l3 != l2); g_assert(list_elem(l) == e1); g_assert(list_elem(l2) == e2); g_assert(list_elem(l3) == e3); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e3); g_assert(list_elem(list_next(l3)) == e1); g_assert(list_elem(list_prev(l3)) == e2); g_assert(list_elem(list_next(list_next(l))) == e3); g_assert(list_elem(list_next(list_prev(l))) == e1); l3 = list_pop(l3); g_assert(l3 == l2); g_assert(list_head(l) == l2); g_assert(list_next(l2) == l); g_assert(list_prev(l2) == l); g_assert(list_next(l) == l2); g_assert(list_prev(l) == l2); l2 = list_pop(l2); g_assert(list_head(l) == l); g_assert(l == l2); g_assert(list_next(l) == l2); g_assert(list_prev(l) == l2); g_assert(list_insert(l, e2) == l); l2 = list_last(l); g_assert(list_insert(l2, e2) == l); l3 = list_last(l); g_assert(l != l2); g_assert(l2 != l3); g_assert(l != l3); g_assert(list_prev(l) == l3); g_assert(list_next(l) == l2); g_assert(list_next(l2) == l3); g_assert(list_head(l) == l); g_assert(l2 == list_roll(l3)); g_assert(l3 == list_roll(l3)); g_assert(l == list_roll(l3)); g_assert(l3 == list_back_roll(l3)); g_assert(l2 == list_back_roll(l3)); g_assert(l == list_back_roll(l3)); ysCall(ygGetTccManager(), "list_destroy", l); l = list_init_from_array(gc, gc, "list :p"); g_assert(l); g_assert(list_elem(l) == e1); g_assert(list_elem(list_next(l)) == e2); g_assert(list_elem(list_prev(l)) == e3); g_assert(list_elem(list_next(list_next(l))) == e3); g_assert(list_elem(list_next(list_prev(l))) == e1); g_assert(yeGet(gc, "list :p") == l); Entity *ar2 = list_to_array(l, gc, "ar2"); g_assert(ar2 && ar2 == yeGet(gc, "ar2")); g_assert(yeGet(ar2, 0) == e1); g_assert(yeGet(ar2, 1) == e2); g_assert(yeGet(ar2, 2) == e3); ysCall(ygGetTccManager(), "list_destroy", l); ygCleanGameConfig(&cfg); yeDestroy(gc); ygEnd(); }