int IndieLib() { //-------------------Constants------------------ enum emode {menu, create, config}; const char FPS = 60; const float dcam = 0.05f; const char prompt[] = ">\t"; //----------------Logic variables--------------- bool show_b_areas = true; emode mode = create; emode prev_mode = create; float dm = 0; //--------------------Engine-------------------- CIndieLib *prog = CIndieLib::Instance(); if (!prog->Init()) return 1; prog->Window->SetTitle("Level Editor 1.1-alpha"); //prog->Window->Cursor(true); // Render timer IND_Timer render_timer; int ticks = 0; int last_frame = 0; render_timer.Start(); // Camera float cam_x = (float) prog->Window->GetWidth() / 2; float cam_y = (float) prog->Window->GetHeight() / 2; IND_Camera2d cam((int) cam_x, (int) cam_y); IND_Camera2d bcam((int) cam_x, (int) cam_y); //-------------------Resources------------------ IND_Font t_font; IND_Entity2d menu_e; try { if (!prog->FontManager->Add(&t_font, "..\\res\\font_small.png", "..\\res\\font_small.xml", IND_ALPHA, IND_32)) throw res_error(); if (!prog->Entity2dManager->Add(4, &menu_e)) throw res_error(); } catch (res_error) { prog->End(); return 2; } //-----------Menu and additional info----------- cmd_line cmd_menu; char cmd_str[520]; char buf[10]; strcpy(cmd_str, prompt); menu_e.SetFont(&t_font); menu_e.SetAlign(IND_LEFT); menu_e.SetCharSpacing(-8); menu_e.SetLineSpacing(18); menu_e.SetText(cmd_str); menu_e.SetPosition(5, (float) (prog->Window->GetHeight() - 25), 0); menu_e.SetShow(false); // Information info tile_info; prog->Entity2dManager->Add(4, &tile_info.get_entity()); tile_info.set_font(t_font); info lvl_info; bool update_info = true; prog->Entity2dManager->Add(4, &lvl_info.get_entity()); lvl_info.set_font(t_font); lvl_info.set_position(prog->Render->GetViewPortX() + 5, prog->Render->GetViewPortY() + 5, 0); lvl_info.add_text(unnamed_lt_file); //------------------Tiles manager--------------- lt_manager ltm; try { if (!ltm.load_textures("..\\textures")) throw tm_init_fail(); } catch (tm_init_fail) { prog->End(); return 3; } //-------------Cofiguration manager------------- conf_manager conf_m; //-----------------Tile cursor------------------ tile_cursor tcursor; float cur_dx = 0; float cur_dy = 0; try { if (!tcursor.init(*prog)) throw cursor_init_fail(); } catch (cursor_init_fail) { prog->End(); return 3; } tcursor.set_textures(ltm.get_textures()); tcursor.set_tiles(ltm.get_tiles(0)); //-----------------Config cursor---------------- config_cursor ccursor(conf_m); try { if (!ccursor.init(*prog)) throw cursor_init_fail(); } catch (cursor_init_fail) { prog->End(); return 3; } ccursor.hide(); editing_cursor *cursor; if (mode == create) cursor = &tcursor; lvl_info.add_text("\nTile set: "); lvl_info.add_text(tcursor.cur_tiles().get_name()); //-------------------Main loop------------------ while (!prog->Input->OnKeyPress(IND_ESCAPE) && !prog->Input->Quit()) { // Input update prog->Input->Update(); // Count time for next rendering ticks = render_timer.GetTicks(); // ~ button press (show command line) if (prog->Input->OnKeyPress(IND_BACKQUOTE) && !tcursor.active_tile()) { if (!menu_e.IsShow()) { prev_mode = mode; mode = menu; } else mode = prev_mode; menu_e.SetPosition((float) (cam_x - prog->Window->GetWidth() / 2 + 5), (float) (cam_y + prog->Window->GetHeight() / 2 - 25), 0); menu_e.SetShow(!menu_e.IsShow()); } // Changing level info position lvl_info.set_position((int) cam_x - prog->Window->GetWidth() / 2 + 5, (int) cam_y - prog->Window->GetHeight() / 2 + 5, 0); // Changing active tile info position tile_info.set_position((int) cam_x + prog->Window->GetWidth() / 2 - 100, (int) cam_y - prog->Window->GetHeight() / 2 + 5, 0); //---------------Updating input due to active mode--------------- switch (mode) { case menu: { // Menu input update char ch = '\0'; for (char key = 97; key <= 122; key++) if (prog->Input->OnKeyPress(key - 97)) ch = key; for (char key = 48; key <= 57; key++) if (prog->Input->OnKeyPress(key - 22)) ch = key; if (prog->Input->OnKeyPress(IND_SPACE)) ch = ' '; if (prog->Input->OnKeyPress(IND_MINUS)) ch = '-'; //if (prog->Input->OnKeyPress(IND_AT)) ch = ':'; //if (prog->Input->OnKeyPress(IND_CARET)) ch = '_'; if (prog->Input->OnKeyPress(IND_BACKSLASH)) ch = '\\'; if (prog->Input->OnKeyPress(IND_PERIOD)) ch = '.'; if (prog->Input->OnKeyPress(IND_BACKSPACE) || prog->Input->IsKeyPressed(IND_BACKSPACE, 100)) { cmd_menu.pop_back(); strcpy(cmd_str, prompt); strcat(cmd_str, cmd_menu.get_string()); } if (ch) { cmd_menu.push_back(ch); strcpy(cmd_str, prompt); strcat(cmd_str, cmd_menu.get_string()); } // Performing command if (prog->Input->OnKeyPress(IND_RETURN)) { if (perform_command(cmd_menu.parse(), ltm, conf_m)) { strcpy(cmd_str, prompt); menu_e.SetShow(false); mode = prev_mode; update_info = true; } else { strcpy(cmd_str, prompt); strcat(cmd_str, "bad command"); } cmd_menu.clear(); } } break; case create: { // Cursor update cursor_update(*prog, cursor, cur_dx, cur_dy); tcursor_update(*prog, tcursor); // 1 button press (set ground as current tile set) if (prog->Input->OnKeyPress(IND_1) && !tcursor.active_tile()) { tcursor.set_tiles(ltm.get_tiles(0)); update_info = true; } // 2 button press (set terrain as current tile set) if (prog->Input->OnKeyPress(IND_2) && !tcursor.active_tile()) { tcursor.set_tiles(ltm.get_tiles(1)); update_info = true; } // 3 button press (set items as current tile set) if (prog->Input->OnKeyPress(IND_3) && !tcursor.active_tile()) { tcursor.set_tiles(ltm.get_tiles(2)); update_info = true; } } break; case config: { cursor_update(*prog, cursor, cur_dx, cur_dy); } break; } // Non menu actions if (mode != menu) { // Tab button press (show/hide bounding areas OR switch working mode) if (prog->Input->OnKeyPress(IND_TAB)) if (prog->Input->IsKeyPressed(IND_LCTRL)) { switch (mode) { case create: if (*ltm.get_lt_file() && !cursor->active_tile()) { mode = config; cursor = &ccursor; tcursor.hide(); if (conf_m.get_doors_count()) conf_m.update(); else conf_m.create(ltm.get_textures(), ltm.get_tiles(1)); } break; case config: mode = create; cursor = &tcursor; ccursor.hide(); break; } cursor->show(); update_info = true; } else show_b_areas = !show_b_areas; // Camera update camera_update(*prog, cam, bcam, cur_dx, cur_dy, cam_x, cam_y, dm * dcam); } // Editor info if (update_info) { lvl_info.set_text("Mode: "); if (mode == create) lvl_info.add_text("create"); else lvl_info.add_text("config"); lvl_info.add_text("\nFile: "); if (*ltm.get_lt_file()) lvl_info.add_text(ltm.get_lt_file()); else lvl_info.add_text(unnamed_lt_file); lvl_info.add_text("\nTile set: "); lvl_info.add_text(tcursor.cur_tiles().get_name()); update_info = false; } // Tile info if (tcursor.active_tile()) { tile_info.set_text("Z pos: "); tile_info.add_text(itoa(tcursor.active_tile()->GetPosZ(), buf, 10)); tile_info.add_text("\nZ angle: "); tile_info.add_text(itoa((int) tcursor.active_tile()->GetAngleZ(), buf, 10)); tile_info.add_text("\nScale: "); tile_info.add_text(itoa((int) tcursor.active_tile()->GetScaleX(), buf, 10)); tile_info.add_text("."); if ((int) (tcursor.active_tile()->GetScaleX() * 10) % 10 == 0) tile_info.add_text("0"); if (tcursor.active_tile()->GetScaleX() * 100 < 100) tile_info.add_text(itoa((int) (tcursor.active_tile()->GetScaleX() * 100), buf, 10)); else tile_info.add_text(itoa((int) (tcursor.active_tile()->GetScaleX() * 100) % 100, buf, 10)); } else { tile_info.set_text(""); } // Rendering if (ticks > last_frame + 1000/FPS) { // Viewport & camera prog->Render->ClearViewPort(0, 0, 0); prog->Render->SetViewPort2d(0, 0, prog->Window->GetWidth(), prog->Window->GetHeight()); prog->Render->SetCamera2d(&cam); // Scene prog->Render->BeginScene(); prog->Entity2dManager->RenderEntities2d(background_layer); prog->Entity2dManager->RenderEntities2d(objects_layer); prog->Entity2dManager->RenderEntities2d(items_layer); if (show_b_areas) { prog->Entity2dManager->RenderCollisionAreas(background_layer, 0, 0, 255, 255); prog->Entity2dManager->RenderCollisionAreas(objects_layer, 255, 0, 0, 255); prog->Entity2dManager->RenderCollisionAreas(items_layer, 0, 255, 0, 255); } prog->Render->SetCamera2d(&bcam); prog->Entity2dManager->RenderEntities2d(4); prog->Entity2dManager->RenderEntities2d(5); prog->Entity2dManager->RenderCollisionAreas(5, 255, 0, 0, 255); if (mode == config) conf_m.render_links(*prog->Render); prog->Render->EndScene(); dm = prog->Render->GetFrameTime() / 1000.0f; last_frame = ticks; } } // Finishing render_timer.Stop(); prog->End(); return 0; }
/* ================== Main ================== */ int IndieLib() { // ----- IndieLib intialization ----- CIndieLib *mI = CIndieLib::Instance(); if (!mI->Init ()) return 0; // ----- Surface loading ----- // 3d Dino loading IND_3dMesh mMeshDino; if (!mI->MeshManager->Add (&mMeshDino, "..\\resources\\trex dx\\dino videogame.x", "..\\resources\\trex dx")) return 0; // Font IND_Font mFontSmall; if (!mI->FontManager->Add (&mFontSmall, "..\\resources\\font_small.png", "..\\resources\\font_small.xml", IND_ALPHA, IND_32)) return 0; // ----- Font creation ----- IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mTextSmallWhite); // Entity adding mTextSmallWhite.SetFont (&mFontSmall); // Set the font into the entity mTextSmallWhite.SetLineSpacing (18); mTextSmallWhite.SetCharSpacing (-8); mTextSmallWhite.SetPosition (5, 5, 1); mTextSmallWhite.SetAlign (IND_LEFT); // ----- Set the mesh into 3d entity ----- // Creating 3d entity IND_Entity3d mDino; mI->Entity3dManager->Add (&mDino); // Entity adding mDino.Set3dMesh (&mMeshDino); // Set the 3d mesh into the entity // ----- Cameras ------ IND_Camera2d mCamera2d (mI->Window->GetWidth () / 2, mI->Window->GetHeight() / 2); IND_Camera3d mCamera3d (0.0f, 0.0f, -2.0f); mCamera3d.SetAspect ((float) mI->Window->GetWidth () / mI->Window->GetHeight()); // ----- Light ----- IND_Light mLight0; mI->LightManager->Add (&mLight0, IND_AMBIENT_LIGHT); mLight0.SetColor (1.0f, 1.0f, 1.0f, 1.0f); // Light 1 (Direction light) IND_Light mLight1; mI->LightManager->Add (&mLight1, IND_DIRECTIONAL_LIGHT); mLight1.SetColor (1.0f, 1.0f, 1.0f, 1.0f); mLight1.SetDirection (0.0f, -0.3f, 0.5f); mLight1.SetRange (1000.0f); // Light 2 (Point light) IND_Light mLight2; mI->LightManager->Add (&mLight2, IND_POINT_LIGHT); mLight2.SetPosition (3, 3, 3); mLight2.SetColor (0.4f, 1.0f, 0.4f, 1.0f); mLight2.SetRange (200); mLight2.SetAttenuation (0.5f); // Light 3 (Spot light) IND_Light mLight3; mI->LightManager->Add (&mLight3, IND_SPOT_LIGHT); mLight3.SetPosition (5, 5, 5); mLight3.SetColor (1.0f, 1.0f, 1.0f, 1.0f); mLight3.SetDirection (0.0f, -0.3f, 0.5f); mLight3.SetRange (1000); mLight3.SetAttenuation (0.2f); mLight3.SetFalloff (1.0f); mLight3.SetPhi (8.0f); mLight3.SetTheta (7); // ----- Main Loop ----- mI->LightManager->Disable(&mLight2); mI->LightManager->Disable(&mLight3); float mAngle = 0; char mText [2048]; mText [0] = 0; int mSpeed = 25; float mDelta; while (!mI->Input->OnKeyPress (IND_ESCAPE) && !mI->Input->Quit()) { // ----- Input update ---- mI->Input->Update (); // ----- Text ----- strcpy (mText, "Press 1, 2 or 3 in order to toggle between different lights\n"); mTextSmallWhite.SetText (mText); // ----- Input ----- mDelta = mI->Render->GetFrameTime() / 1000.0f; // Activate only light 1 if (mI->Input->IsKeyPressed (IND_1)) { mI->LightManager->Disable(&mLight2); mI->LightManager->Disable(&mLight3); mI->LightManager->Enable(&mLight1); } // Activate only light 2 if (mI->Input->IsKeyPressed (IND_2)) { mI->LightManager->Disable(&mLight1); mI->LightManager->Disable(&mLight3); mI->LightManager->Enable(&mLight2); } // Activate only light 3 if (mI->Input->IsKeyPressed (IND_3)) { mI->LightManager->Disable(&mLight1); mI->LightManager->Disable(&mLight2); mI->LightManager->Enable(&mLight3); } // ----- Updating entities attributes ----- mAngle += mDelta * mSpeed; mDino.SetAngleXYZ (0, mAngle, 0); // ----- Render ----- mI->Render->ClearViewPort (60, 60, 60); mI->Render->BeginScene (); mI->Render->SetViewPort3d (0, 0, mI->Window->GetWidth (), mI->Window->GetHeight()); mI->Render->SetCamera3d (&mCamera3d); mI->LightManager->Update (); mI->Entity3dManager->RenderEntities3d (); mI->Render->SetViewPort2d (0, 0, mI->Window->GetWidth (), mI->Window->GetHeight ()); mI->Render->SetCamera2d (&mCamera2d); mI->Entity2dManager->RenderEntities2d (); mI->Render->EndScene (); } // ----- Free ----- mI->End (); return 0; }