#include "jgame.h" #include "jdraw.h" #include "jinput.h" #include #include "actor.h" #include "room.h" #include "jui.h" #include #include draw::surface *surf; int room_w = 2; int room_h = 2; int room_xp = -1; int room_xn = -1; int room_yp = -1; int room_yn = -1; int room_color = 9; int room_floor = 0; int room_walls = 0; int room_doors = 0; int room_walldoors = 0; std::vector gifs; bool editing = false; void restart() { room::load(0); //room_w, room_h, room_xp, room_xn, room_yp, room_yn, room_color, room_floor, room_walls, room_doors, room_walldoors); /* box = actor::create("ASCENSOR",{16,32,0}, {8,8,4}, {64,0,32,24}, {0,24}); box->flags = FLAG_MOVING; box->movement = MOV_Z; box->mov_push = PUSH_ZP; actor::setDirty(box, true); */ //box = actor::create("HERO", {16,32,8}, {8,8,12}, {0,32,20,32}, {-6,38}); //box->flags = FLAG_HERO | FLAG_PUSHABLE | FLAG_GRAVITY | FLAG_ORIENTABLE | FLAG_ANIMATED; //actor::setDirty(box, true); //actor::reorder(); } void game::init() { draw::init("The Pool", 520, 240, 3); //room::init(); surf = draw::loadSurface("test.gif"); draw::setSource(surf); draw::loadPalette("test.gif"); game::setUpdateTicks(64); FILE* f = fopen("data/gifs.txt", "r"); int buffer_len=255; char buffer[buffer_len]; while(fgets(buffer, buffer_len, f)) { auto len = strlen(buffer); buffer[len-1]=0; gifs.push_back(std::string(buffer)); } actor::actor_t *hero = actor::create("HERO", {16,32,8}, {8,8,12}, "test.gif", {0,32,20,32}, {-6,38}); //actor::actor_t *hero = actor::create("HEROHEROHEROHER", {16,32,8}, {8,8,12}, "abad.gif", {0,0,20,35}, {-6,32}); hero->flags = FLAG_HERO | FLAG_PUSHABLE | FLAG_GRAVITY | FLAG_ORIENTABLE | FLAG_ANIMATED; actor::setDirty(hero, true); restart(); /* actor::actor_t *box = actor::create("BOX", {32,32,16}, {8,8,8}, "test.gif", {32,0,32,32}, {0,32}); box->flags = FLAG_PUSHABLE | FLAG_GRAVITY; box->movement = MOV_CW; box->mov_push = PUSH_XN; actor::setDirty(box, true); box = actor::create("BOX2", {32,32,0}, {8,8,8}, "test.gif", {32,0,32,32}, {0,32}); box->flags = FLAG_PUSHABLE | FLAG_GRAVITY; box->movement = MOV_CW; box->mov_push = PUSH_XN; actor::setDirty(box, true); */ actor::select(actor::find("BOX")); } int sx=1, sy=0; void print(int x, int y, int num) { int digits=0; bool sign = num < 0; num = SDL_abs(num); int n = num; while (n>0) {n=n/10;digits++;} if (sign) digits++; x=x+digits*4; if (num==0) draw::draw(x+4,y,5,7,0,120); while (num>0) { draw::draw(x,y,5,7,(num%10)*5,120); num=num/10; x=x-4; } if (sign) draw::draw(x,y,5,7,50,120); } void btn(const char* label, const int x, const int y, int &var, int min, int max) { char buffer[100]; int result=0; draw::print(label, x, y+3, 15, 0); result=ui::button(SDL_itoa(var, buffer, 10), x+50, y, 28, 11, TEAL, LIGHT+TEAL, LIGHT+PURPLE); if (result) { var=SDL_max(min, SDL_min(max, var-(result-2))); room::editor::setWidth(var); } } void btn_small(const int x, const int y, int &var, int min, int max, bool restrt=false) { char buffer[100]; int result=0; result=ui::spin(SDL_itoa(var, buffer, 10), x, y, 17, 11); if (result) { var=SDL_max(min, SDL_min(max, var+result)); if (restrt) restart(); } } void btn_check(const int x, const int y, const char* label, uint8_t &flags, const uint8_t value) { int result=0; if (flags & value) { result=ui::check(label, x, y, 12, 11, true); } else { result=ui::check(label, x, y, 12, 11, false); } if (result) { if (flags & value) { flags = flags & ~value; } else { flags = flags | value; } } } void btn_check(const int x, const int y, const char* label, uint16_t &flags, const uint16_t value) { int result=0; if (flags & value) { result=ui::check(label, x, y, 24, 11, true); } else { result=ui::check(label, x, y, 24, 11, false); } if (result) { if (flags & value) { flags = flags & ~value; } else { flags = flags | value; } } } void btn_opt(const char* label, const int x, const int y, uint8_t &var, std::vector values, std::vector labels) { //draw::print(label, x, y+3, 15, 0); ui::label(label, x, y, 32, 11); int result = 0; std::size_t pos = 0; while (pos values) { //draw::print(label, x, y+3, PAPER, 0); ui::label(label, x, y, 32, 11); int result = 0; std::size_t pos = 0; std::string v = var; while (pos < values.size() && v != values[pos]) { pos++; } if (pos==values.size()) pos = 0; result = ui::combo(var, x+31, y, 65, 11); if (result) { pos++; if (pos==values.size()) pos=0; strcpy(var,values[pos].c_str()); return true; } return false; } const uint8_t scancode_to_char(const uint8_t scancode) { if (scancode == SDL_SCANCODE_0) return '0'; if (scancode >= SDL_SCANCODE_1 && scancode <= SDL_SCANCODE_9) return scancode+19; if (scancode >= SDL_SCANCODE_A && scancode <= SDL_SCANCODE_Z) return scancode+61; return 32; } void btn_txt(const char* label, const int x, const int y, char *var) { //draw::print(label, x, y+3, PAPER, 0); ui::label(label, x, y, 32, 11); int result = 0; result = ui::textbox(var, x+31, y, 65, 11); if (result) { const int len = strlen(var); if (result == SDL_SCANCODE_BACKSPACE) { if (len>0) var[len-1] = 0; } else { if (len<15) { var[len] = scancode_to_char(result); var[len+1] = 0; } } } } int section = 0; void editor_move_selected() { actor::actor_t *selected = actor::getSelected(); if (!selected) return; vec3_t min = room::getMin(); vec3_t max = room::getMax(); if ( input::keyDown(SDL_SCANCODE_LEFT) && selected->pos.x>min.x ) { selected->pos.x--; actor::setDirty(selected); } if ( input::keyDown(SDL_SCANCODE_RIGHT) && selected->pos.xpos.x++; actor::setDirty(selected); } if ( input::keyDown(SDL_SCANCODE_UP) && selected->pos.y>min.y ) { selected->pos.y--; actor::setDirty(selected); } if ( input::keyDown(SDL_SCANCODE_DOWN) && selected->pos.ypos.y++; actor::setDirty(selected); } if ( input::keyDown(SDL_SCANCODE_PAGEDOWN) && selected->pos.z>min.z ) { selected->pos.z--; actor::setDirty(selected); } if ( input::keyDown(SDL_SCANCODE_PAGEUP) /*&& selected->pos.zpos.z++; actor::setDirty(selected); } } bool game::loop() { if (input::keyDown(SDL_SCANCODE_ESCAPE)) return false; // WHILE EDITING... if (editing) { editor_move_selected(); actor::updateEditor(actor::getFirst()); } else { actor::update(actor::getFirst()); } actor::reorder(); draw::resetViewport(); draw::cls(2); draw::setViewport(100,0,320,240); room::draw(); actor::draw(actor::getFirst()); room::draw2(); //draw::draw(148+sx*2-sy*2, 67+sx+sy,24,24,24,0); /* print(0,0,input::mouseX()); print(0,20,input::mouseY()); print(0,30,input::mouseBtn(1)?1:0); print(0,40,input::mouseBtn(2)?1:0); print(0,50,input::mouseBtn(3)?1:0); */ /* if (section==0) { draw::print("ROOM", 334, 13, YELLOW, LIGHT+BLACK); if (ui::button("ACTORS", 365, 10, 30, 11, TEAL, LIGHT+TEAL, LIGHT+PURPLE)) section=1; } else { if (ui::button("ROOM", 330, 10, 30, 11, TEAL, LIGHT+TEAL, LIGHT+PURPLE)) section=0; draw::print("ACTORS", 369, 13, YELLOW, BLACK); } */ actor::actor_t *act = nullptr; /* switch (section) { case 0: */ draw::setViewport(0,0,100,240); draw::color(WHITE); draw::fillrect(0, 0, 100, 240); btn("ROOM WIDTH:", 10, 40, room_w, 0, 3); btn("ROOM HEIGHT:", 10, 55, room_h, 0, 3); draw::print("DOORS:", 10, 80, 15, 0); btn_small(32, 80, room_xn, -1, 5, true); btn_small(49, 80, room_yp, -1, 5, true); btn_small(66, 80, room_xp, -1, 5, true); btn_small(83, 80, room_yn, -1, 5, true); btn("COLOR:", 10, 140, room_color, 5, 11); btn("FLOOR:", 10, 155, room_floor, 0, 5); btn("WALLS:", 10, 170, room_walls, 0, 5); btn("DOORS:", 10, 185, room_doors, 0, 4); btn("WDOORS:", 10, 200, room_walldoors, 0, 5); /* break; case 1: */ draw::setViewport(420, 0, 100, 240); draw::color(WHITE); draw::fillrect(0, 0, 100, 240); draw::setViewport(420, 2, 100, 238); draw::color(LIGHT+WHITE); draw::fillrect(2, 0, 96, 100); draw::color(PAPER); draw::rect(2, 0, 96, 100); const int mx = draw::getLocalX(input::mouseX()); const int my = draw::getLocalY(input::mouseY()); const bool btnDown = input::mouseBtn(1) || input::mouseBtn(3); int line = 0; act = actor::getFirst(); while (act) { if ((act->flags&FLAG_NOEDITOR)!=FLAG_NOEDITOR) { if (act==actor::getSelected()) { draw::color(LIGHT+BLUE); draw::fillrect(4, 2+line*9, 92, 9); } if ((mx>=2) && (mx<98) && (my>=2+line*9) && (my<2+9+line*9) && btnDown) { actor::select(act); //section=1; } draw::print(act->name, 6, 4+line*9, LIGHT+WHITE, BLACK); line++; } act = act->next; } act = actor::getSelected(); if (act) { draw::setViewport(420, 90, 100, 150); btn_txt("NAME:", 2, 0, act->name); if (btn_opt2("BMP:", 2, 10, act->bmp, gifs)) { draw::freeSurface(act->surface); act->surface = draw::loadSurface(act->bmp); } //draw::print("RECT:", 2, 64, PAPER, 0); ui::label("RECT", 2, 20, 32, 11); btn_small(33, 20, act->bmp_rect.x, 0, 512); btn_small(49, 20, act->bmp_rect.y, 0, 512); btn_small(65, 20, act->bmp_rect.w, 0, 512); btn_small(81, 20, act->bmp_rect.h, 0, 512); ui::label("OFFSET", 2, 30, 32, 11); btn_small(65, 30, act->bmp_offset.x, -32, 64); btn_small(81, 30, act->bmp_offset.y, -32, 64); ui::label("POS", 2, 42, 32, 11); btn_small(49, 42, act->pos.x, 0, 512); btn_small(65, 42, act->pos.y, 0, 512); btn_small(81, 42, act->pos.z, 0, 512); ui::label("SIZE", 2, 52, 32, 11); btn_small(49, 52, act->size.x, 0, 512); btn_small(65, 52, act->size.y, 0, 512); btn_small(81, 52, act->size.z, 0, 512); btn_check(2, 64, "HERO", act->flags, FLAG_HERO); btn_check(26, 64, "PUSH", act->flags, FLAG_PUSHABLE); btn_check(50, 64, "REAC", act->flags, FLAG_REACTIVE); btn_check(74, 64, "MOVI", act->flags, FLAG_MOVING); btn_check(2, 74, "ANIM", act->flags, FLAG_ANIMATED); btn_check(26, 74, "ORIE", act->flags, FLAG_ORIENTABLE); btn_check(50, 74, "DEAD", act->flags, FLAG_DEADLY); btn_check(74, 74, "GRAV", act->flags, FLAG_GRAVITY); btn_opt("ORIENT", 2, 86, act->orient, {PUSH_NONE, PUSH_XP, PUSH_XN, PUSH_YP, PUSH_YN, PUSH_ZP, PUSH_ZN}, {"NONE", "XP", "XN", "YP", "YN", "ZP", "ZN"}); btn_opt("MOVEMNT", 2, 96, act->movement, {MOV_NONE, MOV_X, MOV_Y, MOV_Z, MOV_CW, MOV_CCW, MOV_RAND, MOV_HUNT}, {"NONE", "X", "Y", "Z", "CW", "CCW", "RAND", "HUNT"}); btn_opt("ANIMCYC", 2, 106, act->anim_cycle, {0, 1}, {"0 1 0 2", "0 1 2 3"}); //draw::print("ANIM SPEED:", 2, 156, 15, 0); ui::label("ANMSPED", 2, 116, 32, 11); btn_small(81, 116, act->anim_wait, 0, 10); ui::label("RMASK", 2, 128, 24, 11); btn_check(24+2, 128, "XP", act->react_mask, PUSH_XP); btn_check(24+14, 128, "XN", act->react_mask, PUSH_XN); btn_check(24+26, 128, "YP", act->react_mask, PUSH_YP); btn_check(24+38, 128, "YN", act->react_mask, PUSH_YN); btn_check(24+50, 128, "ZP", act->react_mask, PUSH_ZP); btn_check(24+62, 128, "ZN", act->react_mask, PUSH_ZN); ui::label("RPUSH", 2, 140, 24, 11); btn_check(24+2, 140, "XP", act->react_push, PUSH_XP); btn_check(24+14, 140, "XN", act->react_push, PUSH_XN); btn_check(24+26, 140, "YP", act->react_push, PUSH_YP); btn_check(24+38, 140, "YN", act->react_push, PUSH_YN); btn_check(24+50, 140, "ZP", act->react_push, PUSH_ZP); btn_check(24+62, 140, "ZN", act->react_push, PUSH_ZN); /* break; } */ /* case 2: draw::setViewport(320, 38, 100, 100); draw::color(LIGHT+WHITE); draw::fillrect(2, 0, 96, 100); draw::color(PAPER); draw::rect(2, 0, 96, 100); const int mx = draw::getLocalX(input::mouseX()); const int my = draw::getLocalY(input::mouseY()); const bool btnDown = input::mouseBtn(1) || input::mouseBtn(3); int line = 0; act = actor::getFirst(); while (act) { if ((act->flags&FLAG_NOEDITOR)!=FLAG_NOEDITOR) { if (act==actor::getSelected()) { draw::color(LIGHT+BLUE); draw::fillrect(4, 2+line*9, 92, 9); } if ((mx>=2) && (mx<98) && (my>=2+line*9) && (my<2+9+line*9) && btnDown) { actor::select(act); //section=1; } draw::print(act->name, 6, 4+line*9, LIGHT+WHITE, BLACK); line++; } act = act->next; } break; */ } draw::render(); return true; }