#include #include #include #include "polygon.h" #include "circle.h" #include "collisions.h" #include #include using namespace wgd; using std::list; class MyGame : public Game { private: bool paused; bool dragging; Body* selected; Vector2D offset; int createIndex; bool mousePressed; doste::OID keyStates; std::list bodies; public: MyGame () : Game ("data/load.dst") {} bool pressed (const doste::OID& key) { if (Keyboard::keyPressed(key)) { if (! keyStates[key]) { keyStates.set(key, true); return true; } } else { keyStates.set(key, false); } return false; } void initialise() { Instance::regInstance(); Instance::regInstance(); Game::fixedTimestep(1.0 / 60.0); selected = 0; createIndex = 0; dragging = false; mousePressed = false; keyStates = DB::createObject(); Mouse::visible(false); DB::runScript("data/scene.dst"); DB::runScript("data/script0.dst"); DB::runScript("data/script1.dst"); DB::runScript("data/script2.dst"); DB::runScript("data/script3.dst"); }; //Main update loop - all game logic and workings go in this function // do not draw stuff in this function void update() { char fps[20]; sprintf(fps, "%.1f", getFPS()); WGD[ix::widgets]["fpscounter"].set(ix::caption, dstring(fps)); //If escape is pressed then quit the game. if (Keyboard::keyPressed(Keyboard::KEY_ESCAPE)) quit(); if (pressed('p')) { WGD.set(ix::paused, ! WGD[ix::paused]); } bool pause = WGD[ix::paused]; if (pressed(' ')) { pause = false; } if (pressed('r')) { DB::runScript("data/scene.dst"); selected = 0; } for (int i = 0; i < 10; i++) { char c = i + 48; if (pressed(c)) { char file[] = "data/scripti.dst"; file[11] = c; wgd::cout << file << "\n"; DB::runScript(file); } } float density; if (selected) { density = selected->getDensity(); selected->setDensity(Infinity); } bodies.clear(); for(Scene::Iterator i = Scene::current()->begin(); iselected(false); /*std::cerr << "x: " << (*i)->getX(); std::cerr << ", y: " << (*i)->getY(); std::cerr << ", rot: " << (*i)->getRotation(); std::cerr << "\n";*/ } int x = Mouse::x(); int y = GameWindow::height() - Mouse::y(); bool mouseClicked = false; if (Mouse::btnPressed(Mouse::BTN_LEFT)) { if (! mousePressed) { mouseClicked = true; } mousePressed = true; } else { mousePressed = false; } Vector2D mouse(x, y); list::iterator it1; list::iterator it2; if (! pause) { for (it1 = bodies.begin(); it1 != bodies.end(); it1++) { (*it1)->update(); } } if (selected) { selected->selected(true); } if (Keyboard::keyPressed(Keyboard::KEY_CTRL)) { if (! selected) { selected = Instance::create(createIndex++); density = selected->getDensity(); selected->setDensity(Infinity); WGD.set("selected", selected->getID()); } Polygon* p = dynamic_cast(selected); if (p && mouseClicked) { p->addVertex(x, y); } } else if (Keyboard::keyPressed(Keyboard::KEY_SHIFT)) { if (! selected) { selected = Instance::create(createIndex++); selected->position(mouse); density = selected->getDensity(); selected->setDensity(Infinity); WGD.set("selected", selected->getID()); } Circle* c = dynamic_cast(selected); if (c) { c->setRadius(Vector2D::distance(mouse, c->position())); } } else { if (pressed('c')) { Circle* c = Instance::create(createIndex++); c->position(mouse); c->setRadius(wgd::random(10, 30)); } if (selected) { if (dragging && Mouse::btnPressed(Mouse::BTN_LEFT)) { Vector2D movement = mouse + offset - selected->position(); selected->position(mouse + offset); selected->setVelocity(movement * (0.1f / Game::frameTime())); } else { dragging = false; } } if (! dragging) { bool mouseOver = false; for (it1 = bodies.begin(); it1 != bodies.end(); it1++) { Body* body = *it1; if (body->contains(mouse)) { body->selected(true); mouseOver = true; if (mouseClicked) { dragging = true; if (selected) { selected->setDensity(density); } selected = body; WGD.set("selected", selected->getID()); density = selected->getDensity(); selected->setDensity(Infinity); offset = body->position() - mouse; body->setVX(0); body->setVY(0); body->setRotationVel(0); } break; } } if (!mouseOver && Mouse::btnPressed(Mouse::BTN_LEFT)) { if (selected) { selected->setDensity(density); WGD.set("selected", Null); } selected = 0; } } } Collisions::init(); for (it1 = bodies.begin(); it1 != bodies.end(); it1++) { for (it2 = it1; it2 != bodies.end(); it2++) { if (it1 == it2) { continue; } Collisions::check(*it1, *it2); } } if (! pause) { Collisions::resolve(); } if (pressed('s')) { density = Infinity; if (selected) { selected->setVX(0); selected->setVY(0); selected->setRotationVel(0); } } if (selected) { selected->setDensity(density); } }; //Main draw loop, This function is where you draw everything in the game. void draw(){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, GameWindow::width(), 0, GameWindow::height(), -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRasterPos2f(0.0,0.0); glEnable(GL_LINE_SMOOTH); glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); //glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glLineWidth (1.5); glDisable(GL_CULL_FACE); for (list::iterator it = bodies.begin(); it != bodies.end(); it++) { (*it)->draw(); } int x = Mouse::x(); int y = GameWindow::height() - Mouse::y(); // Cursor glColor4f(0, 0, 0, 1); glBegin(GL_LINES); glVertex3f(x - 10, y, 0); glVertex3f(x + 10, y, 0); glVertex3f(x, y - 10, 0); glVertex3f(x, y + 10, 0); glEnd(); Collisions::draw(); } //This function gets called when the game exits. void finalise() {} }; int main() { //The main function simply creates the game and runs it. Nothing else is needed here. Game* g = new MyGame(); g->run(); delete g; }