Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

anoid.cc

Go to the documentation of this file.
00001 #include "anoid.hh"
00002 #include "world.hh"
00003 #include <iostream>
00004 #include <stdlib.h>
00005 #include "keyboardmessage.hh"
00006 #include "pointermessage.hh"
00007 #include "xmlconfiguration.hh"
00008 
00009 namespace anoid {
00010     namespace main {
00011 
00012         using namespace std;
00013         using namespace context;
00014         using namespace config;
00015 
00016         Anoid::Anoid(Context *context): _context(context), _world(0) {
00017             if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0) {
00018                 cerr << "SDL initialisation failed: " << SDL_GetError() << endl;
00019                 exit(1);
00020             }
00021 
00022             _context->initWindow(1600, 1200);
00023         }
00024 
00025         Anoid::~Anoid() {
00026             if (_world) delete _world;
00027         }
00028 
00029         void Anoid::loadLevel(Configuration &c) {
00030             _world = new World(c, _context);
00031         }
00032 
00033         void Anoid::runLevel() {
00034             SDL_Event event;
00035             while (SDL_PollEvent(&event)) ;
00036 
00037             while (true) {
00038                 processEvents();
00039                 _context->swapBuffers();
00040                 _world->update();
00041                 _world->redraw();
00042                 _world->fireEvents();
00043             }
00044         }
00045 
00046         void Anoid::processEvents() {
00047             SDL_Event event;
00048 
00049             while (SDL_PollEvent(&event)) {
00050                 switch (event.type) {
00051                     case SDL_KEYDOWN:
00052                         handleKeyDown(&event.key.keysym);
00053                         break;
00054                     case SDL_KEYUP:
00055                         handleKeyUp(&event.key.keysym);
00056                         break;
00057                     case SDL_MOUSEMOTION:
00058                         handlePointerMove(-1,event.motion.xrel,(event.motion.yrel));
00059                         break;
00060                     case SDL_QUIT:
00061                         delete _context;
00062                         break;
00063                 }
00064             }
00065         }
00066 
00067         void Anoid::handleKeyUp(SDL_keysym* keysym) {
00068             KeyboardMessage *k = new KeyboardMessage();
00069             k->setUp();
00070             k->setKey(*keysym);
00071             _world->send(k);
00072         }
00073 
00074         void Anoid::handleKeyDown(SDL_keysym* keysym) {
00075             switch (keysym->sym) {
00076                 case SDLK_ESCAPE:
00077                     delete _context;
00078                     break;
00079                 default:
00080                     KeyboardMessage *k = new KeyboardMessage();
00081                     k->setDown();
00082                     k->setKey(*keysym);
00083                     _world->send(k);
00084                     break;
00085             }
00086         }
00087 
00088         void Anoid::handlePointerMove(signed int id, signed int dx, signed int dy) {
00089             PointerMessage *p = new PointerMessage();
00090             p->setID(id);
00091             p->setMove(dx,dy);
00092             _world->send(p);
00093         }
00094 
00095     };
00096 };

Anoid NG © Michael Westergaard, Martin Stig Stissing, Ronni Michael Laursen, and Kristian Bisgaard Lassen