/** @file demos/main.cpp This is a sample main.cpp to get you started with G3D. It is designed to make writing an application easy. Although the GApp/GApplet infrastructure is helpful for most projects, you are not restricted to using it-- choose the level of support that is best for your project (see the G3D Map in the documentation). @author Morgan McGuire, matrix@graphics3d.com */ #include #include "resource.h" #include "Instance.h" #include "PhysicalInstance.h" #include "TextButtonInstance.h" #if G3D_VER < 61000 #error Requires G3D 6.10 #endif static const float VNUM = 0.01F; static std::string title = ""; static const std::string VERSION = "PRE-ALPHA "; static std::vector instances; static std::vector instances_2D; static Instance* dataModel; static GFontRef fntdominant = NULL; static GFontRef fntlighttrek = NULL; static bool democ = true; static std::string message = ""; static G3D::RealTime messageTime = 0; static int FPSVal[8] = {10, 20, 30, 60, 120, 240, INT_MAX,1}; static int index = 2; static float TIMERVAL = 60.0F; static int SCOREVAL = 0; static int sep = 125; static int spacing = 25; static G3D::TextureRef go = NULL; static G3D::TextureRef go_ovr = NULL; static G3D::TextureRef go_dn = NULL; static float mousex = 0; static float mousey = 0; static int go_id = 0; static int go_ovr_id = 0; static int go_dn_id = 0; static bool mouseButton1Down = false; /** This simple demo applet uses the debug mode as the regular rendering mode so you can fly around the scene. */ class Demo : public GApplet { public: // Add state that should be visible to this applet. // If you have multiple applets that need to share // state, put it in the App. class App* app; Demo(App* app); virtual ~Demo() {} virtual void onInit(); virtual void onLogic(); virtual void onNetwork(); virtual void onSimulation(RealTime rdt, SimTime sdt, SimTime idt); virtual void onGraphics(RenderDevice* rd); virtual void onUserInput(UserInput* ui); virtual void onCleanup(); }; class App : public GApp { protected: void main(); public: SkyRef sky; Demo* applet; App(const GAppSettings& settings); ~App(); }; Demo::Demo(App* _app) : GApplet(_app), app(_app) { } #include std::string Convert (float number){ std::ostringstream buff; buff<name = "undefined"; dataModel->parent = NULL; PhysicalInstance* test = makePart(); test->parent = dataModel; test->color = Color3(0.2F,0.3F,1); test->size = Vector3(24,1,24); test = makePart(); test->parent = dataModel; test->color = Color3(.5F,1,.5F); test->size = Vector3(4,1,2); test->position = Vector3(10,1,0); test = makePart(); test->parent = dataModel; test->color = Color3(.5F,1,.5F); test->size = Vector3(4,1,2); test->position = Vector3(-10,1,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(-7,2,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(7,2,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(-4,3,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(5,3,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(-1,4,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(3,4,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(2,5,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(0,6,0); test = makePart(); test->parent = dataModel; test->color = Color3::gray(); test->size = Vector3(4,1,2); test->position = Vector3(-2,7,0); setDesiredFrameRate(FPSVal[index]); app->debugCamera.setPosition(Vector3(0, 2, 10)); app->debugCamera.lookAt(Vector3(0, 2, 0)); //std::string str = "Dynamica Duomillenium 5 Version " + VERSION + Convert(VNUM); //title = dataModel->name; GApplet::onInit(); } void clearInstances() { for(size_t i = 0; i < instances.size(); i++) delete instances.at(i); delete dataModel; } void OnError(int err, std::string msg = "") { std::string emsg = "An unexpected error has occured and DUOM 5 has to quit. We're sorry!" + msg; clearInstances(); //DialogBox(NULL, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL); MessageBox(NULL, emsg.c_str(),"Dynamica Crash", MB_OK); exit(err); } void Demo::onCleanup() { clearInstances(); } void Demo::onLogic() { // Add non-simulation game logic and AI code here } void Demo::onNetwork() { // Poll net messages here } void Demo::onSimulation(RealTime rdt, SimTime sdt, SimTime idt) { if(dataModel->name != title) { title = dataModel->name; app->renderDevice->setCaption("Game \"" + title + "\""); } } //void readMouseGUIInput() //{ //} double getOSVersion() { OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); std::string version = Convert(osvi.dwMajorVersion) + "." + Convert(osvi.dwMinorVersion); return ::atof(version.c_str()); } void Demo::onUserInput(UserInput* ui) { if (ui->keyPressed(SDLK_ESCAPE)) { // Even when we aren't in debug mode, quit on escape. endApplet = true; app->endProgram = true; } if(ui->keyPressed(SDL_RIGHT_MOUSE_KEY)) { app->debugController.setActive(true); } else if(ui->keyReleased(SDL_RIGHT_MOUSE_KEY)) { app->debugController.setActive(false); } if(ui->keyPressed(SDLK_LSHIFT)) { app->debugController.setMoveRate(20); } else if(ui->keyReleased(SDLK_LSHIFT)) { app->debugController.setMoveRate(10); } if(ui->keyDown(SDLK_LCTRL)) { if(ui->keyPressed('d')) { messageTime = System::time(); if(app->debugMode()) message = "Debug Mode Disabled"; else message = "Debug Mode Enabled"; app->setDebugMode(!app->debugMode()); } } if(ui->keyDown(SDLK_LCTRL)) { if(ui->keyPressed('v')) { messageTime = System::time(); message = convert(getOSVersion()); } } if(ui->keyPressed(SDLK_F8)) { index++; if(index >= 7) { index = 0; } messageTime = System::time(); message = "FPS has been set to " + Convert(FPSVal[index]); setDesiredFrameRate(FPSVal[index]); } mousex = ui->getMouseX(); mousey = ui->getMouseY(); mouseButton1Down = ui->keyDown(SDL_LEFT_MOUSE_KEY); //readMouseGUIInput(); // Add other key handling here } std::string ExePath() { char buffer[MAX_PATH]; GetModuleFileName( NULL, buffer, MAX_PATH ); std::string::size_type pos = std::string( buffer ).find_last_of( "\\/" ); return std::string( buffer ).substr( 0, pos); } std::string GetFileInPath(std::string file) { std::string name = ExePath() + file; struct stat buf; if (stat(name.c_str(), &buf) != -1) { return name; } else OnError(202, " \r\nFile not found: " + name); return NULL; } void makeFlag(Vector3 &vec, RenderDevice* &rd) { Vector3 up = Vector3(vec.x, vec.y+3, vec.z); Draw::lineSegment(G3D::LineSegment::fromTwoPoints(vec, up), rd, Color3::blue()); G3D::Array parray; parray.push(Vector2(up.x, up.y)); parray.push(Vector2(up.x-1, up.y-.5)); parray.push(Vector2(up.x, up.y-1)); Draw::poly2D(parray, rd, Color3::blue()); //rd->pushState(); //rd->beginPrimitive(RenderDevice::QUADS); //rd->setColor(Color4(0,0,1,1)); // rd->sendVertex(up); // rd->sendVertex(Vector3(up.x-1, up.y-1, up.z)); // rd->sendVertex(Vector3(up.x, up.y-2, up.z)); //rd->endPrimitive(); //rd->popState(); } bool mouseInArea(float point1x, float point1y, float point2x, float point2y) { if(mousex >= point1x && mousey >= point1y) { if(mousex < point2x && mousey < point2y) { return true; } } return false; } void Demo::onGraphics(RenderDevice* rd) { LightingParameters lighting(G3D::toSeconds(11, 00, 00, AM)); app->renderDevice->setProjectionAndCameraMatrix(app->debugCamera); // Cyan background app->renderDevice->setColorClearValue(Color3(0.0f, 0.5f, 1.0f)); app->renderDevice->clear(app->sky.isNull(), true, true); if (app->sky.notNull()) { app->sky->render(app->renderDevice, lighting); } // Setup lighting app->renderDevice->enableLighting(); app->renderDevice->setAmbientLightColor(Color3(1,1,1)); Draw::axes(CoordinateFrame(Vector3(0, 0, 0)), app->renderDevice); Draw::sphere(G3D::Sphere(Vector3(0,0,0),3), rd, Color3::red(), Color4::clear()); //makeFlag(Vector3(1, 0.5, 0.5), rd); app->renderDevice->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor)); app->renderDevice->setAmbientLightColor(lighting.ambient); //Draw::box(G3D::Box(Vector3(4.0/2,1.0/2,2.0/2),Vector3(0,0,0)), rd, Color3::gray(), Color4(0,0,0,0)); for(size_t i = 0; i < instances.size(); i++) { Instance* instance = instances.at(i); if(instance->className == "Part" && instance->parent != NULL) { PhysicalInstance* part = (PhysicalInstance*)instance; Vector3 size = part->size; Vector3 pos = part->position; Draw::box(Box(Vector3((pos.x-size.x/2)/2,(pos.y-size.y/2)/2,(pos.z-size.z/2)/2),Vector3((pos.x+size.x/2)/2,(pos.y+size.y/2)/2,(pos.z+size.z/2)/2)), app->renderDevice, part->color, Color4::clear()); } } //Draw::cylinder(G3D::Cylinder::Cylinder(Vector3(0,5,0),Vector3(0,10,0),1),app->renderDevice,Color4(0,0,1,0.5),Color4(0,0,0,0)); app->renderDevice->disableLighting(); if (app->sky.notNull()) { app->sky->renderLensFlare(app->renderDevice, lighting); } app->renderDevice->push2D(); //std::string str = ; int offset = 25; if(app->debugMode()) { offset = 60; } if(System::time() - 3 < messageTime) { fntdominant->draw2D(rd, message, Vector2((rd->getWidth()/2)-(fntdominant->get2DStringBounds(message, 20).x/2),(rd->getHeight()/2)-(fntdominant->get2DStringBounds(message, 20).y/2)), 20, Color3::yellow(), Color3::black()); } fntdominant->draw2D(rd, "Timer: " + Convert(TIMERVAL), Vector2(rd->getWidth() - 120, 0+offset), 20, Color3::fromARGB(0x81C518), Color3::black()); fntdominant->draw2D(rd, "Score: " + Convert(SCOREVAL), Vector2(rd->getWidth() - 120, 25+offset), 20, Color3::fromARGB(0x81C518), Color3::black()); //fntlighttrek->draw2D(rd, "Button: " + button, Vector2(10,30 + offset), 15, Color3::white(), Color3::black()); //GUI Boxes Draw::box(G3D::Box(Vector3(0,offset,0),Vector3(80,330+offset,0)),rd,Color4(0.6F,0.6F,0.6F,0.4F), Color4(0,0,0,0)); Draw::box(G3D::Box(Vector3(0,rd->getHeight() - 120,0),Vector3(80,rd->getHeight(),0)),rd,Color4(0.6F,0.6F,0.6F,0.4F), Color4(0,0,0,0)); Draw::box(G3D::Box(Vector3(rd->getWidth() - 120,rd->getHeight() - 120,0),Vector3(rd->getWidth(),rd->getHeight(),0)),rd,Color4(0.6F,0.6F,0.6F,0.4F), Color4(0,0,0,0)); //Camera menu title fntlighttrek->draw2D(rd, "CameraMenu", Vector2(rd->getWidth()-(fntlighttrek->get2DStringBounds("CameraMenu", 14).x+1),rd->getHeight() - 120), 14, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd, "Model", Vector2(10,rd->getHeight() - (120 - spacing*0)), 12, Color3(0,255,255), Color4(0,0,0,0)); fntlighttrek->draw2D(rd, "Surface", Vector2(10,rd->getHeight() - (120 - spacing*1)), 12, Color3(0,255,255), Color4(0,0,0,0)); fntlighttrek->draw2D(rd, "Color", Vector2(10,rd->getHeight() - (120 - spacing*2)), 12, Color3(0,255,255), Color4(0,0,0,0)); fntlighttrek->draw2D(rd, "Controller", Vector2(10,rd->getHeight() - (120 - spacing*3)), 12, Color3(0,255,255), Color4(0,0,0,0)); fntlighttrek->draw2D(rd, "Hopper", Vector2(10,rd->getHeight() - (120 - spacing*4)), 12, Color3(0,255,255), Color4(0,0,0,0)); //Top menu fntlighttrek->draw2D(rd,"File", Vector2(10+0*sep,0), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"Edit", Vector2(10+1*sep,0), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"View", Vector2(10+2*sep,0), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"Insert", Vector2(10+3*sep,0), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"Format", Vector2(10+4*sep,0), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); //Tools menu Draw::box(G3D::Box(Vector3(5, 165+offset,0),Vector3(75, 165+offset,0)),rd,Color4(0.6F,0.6F,0.6F,0.4F), Color4(0.6F,0.6F,0.6F,0.4F)); fntlighttrek->draw2D(rd,"Group", Vector2(10,170+offset), 12, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"UnGroup", Vector2(10,195+offset), 12, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"Duplicate", Vector2(10,220+offset), 12, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); fntlighttrek->draw2D(rd,"MENU", Vector2(10,305+offset), 16, Color3::white(), Color4(0.5F,0.5F,0.5F,0.5F)); //G3D::GFont::draw2D("Debug Mode Enabled", Vector2(0,30), 20, Color3::white(), Color3::black()); //app->debugFont->draw2D("Dynamica 2004-2005 Simulation Client version " + VERSION + str, Vector2(0,0), 20, Color3::white(), Color3::black()); //app->debugFont->draw2D("Debug Mode Enabled", Vector2(0,30), 20, Color3::white(), Color3::black()); rd->pushState(); /*rd->setTexture(0, go); rd->enableAlphaWrite(); //rd->setTexCoord(0, Vector2(0.0F, 0.0F)); //rd->setTexCoord(0, Vector2(1.0F, 0.0F)); //rd->setTexCoord(0, Vector2(0.0F, 1.0F)); //rd->setTexCoord(0, Vector2(1.0F, 1.0F)); //rd->setTextureCombineMode(0, RenderDevice::CombineMode::TEX_ADD); rd->beginPrimitive(RenderDevice::QUADS); rd->sendVertex(Vector2(10,25)); rd->sendVertex(Vector2(70,25)); rd->sendVertex(Vector2(70,85)); rd->sendVertex(Vector2(10,85)); rd->endPrimitive(); rd->setTexture(0, NULL);*/ rd->beforePrimitive(); glEnable( GL_TEXTURE_2D ); glEnable(GL_BLEND);// you enable blending function glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); if(mouseInArea(10,25,70,85)) { if(mouseButton1Down) glBindTexture( GL_TEXTURE_2D, go_dn_id); else glBindTexture( GL_TEXTURE_2D, go_ovr_id); } else glBindTexture( GL_TEXTURE_2D, go_id); //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBegin( GL_QUADS ); glTexCoord2d(0.0,0.0); glVertex2f( 10, 25 ); glTexCoord2d( 1.0,0.0 ); glVertex2f( 70, 25 ); glTexCoord2d( 1.0,1.0 ); glVertex2f( 70, 85 ); glTexCoord2d( 0.0,1.0 ); glVertex2f( 10, 85 ); glEnd(); glDisable( GL_TEXTURE_2D ); rd->afterPrimitive(); rd->popState(); app->renderDevice->pop2D(); } void App::main() { setDebugMode(false); debugController.setActive(false); // Load objects here go = Texture::fromFile(GetFileInPath("/content/images/Run.png")); go_ovr = Texture::fromFile(GetFileInPath("/content/images/Run_ovr.png")); go_dn = Texture::fromFile(GetFileInPath("/content/images/Run_dn.png")); go_id = go->getOpenGLID(); go_dn_id = go_dn->getOpenGLID(); go_ovr_id = go_ovr->getOpenGLID(); fntdominant = GFont::fromFile(GetFileInPath("/content/font/dominant.fnt")); fntlighttrek = GFont::fromFile(GetFileInPath("/content/font/lighttrek.fnt")); sky = Sky::create(NULL, ExePath() + "/content/sky/"); applet->run(); } App::App(const GAppSettings& settings) : GApp(settings) { applet = new Demo(this); } App::~App() { delete applet; } int main(int argc, char** argv) { GAppSettings settings; //settings.debugFontName = "lighttrek.fnt"; //settings.useNetwork = false; //settings.window.width = 1024; //settings.window.height = 768; if(getOSVersion > 5.0) settings.window.defaultIconFilename = GetFileInPath("/content/images/rico.png"); else settings.window.defaultIconFilename = GetFileInPath("/content/images/rico256c.png"); settings.window.resizable = true; App app = App(settings); //app.window()->setIcon(ExePath() + "/content/images/rico.png"); app.run(); return 0; }