/[projectaxis]/projectaxis/IsoEngine/TileEngineTest.cpp
ViewVC logotype

Diff of /projectaxis/IsoEngine/TileEngineTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by vovansim, Thu May 29 01:47:50 2003 UTC revision 1.5 by vovansim, Sat Jun 7 03:55:54 2003 UTC
# Line 4  Line 4 
4  #include "SDL/SDL.h"  #include "SDL/SDL.h"
5    
6  #include "SDL_Tools.h"  #include "SDL_Tools.h"
 #include "IsoGeometryTools.h"  
7  #include "IsoTileSet.h"  #include "IsoTileSet.h"
8  #include "IsoTilePlotter.h"  #include "IsoCore.h"
9  #include "IsoScroller.h"  #include "IsoRenderer.h"
 #include "IsoTileWalker.h"  
 #include "IsoMouseMap.h"  
10    
11  const int MAP_WIDTH = 20;  const int MAP_WIDTH = 200;
12  const int MAP_HEIGHT = 20;  const int MAP_HEIGHT = 200;
13    
14  SDL_Surface* screen = NULL;  SDL_Surface* screen = NULL;
15    SDL_Surface* frame = NULL;
16    
17  IsoTileSet squareTiles;  //tilesets
18  IsoTileSet tiles;  IsoTileSet tsBack;//background
19  IsoTileSet cursor;  IsoTileSet tsShadow;//tree shadow
20    IsoTileSet tsTree;//tree foreground
21    IsoTileSet tsCursor;//cursor
22    
23  IsoTilePlotter tilePlotter;  IsoTilePlotter tilePlotter;
24  IsoTileWalker tileWalker;  IsoTileWalker tileWalker;
25  IsoScroller scroller;  IsoScroller scroller;
26  IsoMouseMap mouseMap;  IsoMouseMap mouseMap;
27    IsoRenderer renderer;
28    
29  POINT cursorCoords;  POINT cursorCoords;
30  POINT scrollSpeed;  POINT scrollSpeed;
31    bool click = false;
 bool overheadView = false;  
32    
33  int theMap[MAP_HEIGHT][MAP_WIDTH];  int theMap[MAP_HEIGHT][MAP_WIDTH];
34    
35  void renderMap();  //rendering functionprototype
36    void RenderFunc(SDL_Surface* dst, RECT* clipRect, int xDst, int yDst, int xMap, int yMap);
37    
38  void loadTileSets() {  void loadTileSets() {
39          squareTiles.loadTileset("AxisTileSet.bmp");          tsBack.loadTileset("backgroundts.bmp");
40          tiles.loadTileset("Tiles.bmp");          tsShadow.loadTileset("treeshadowts.bmp");
41          cursor.loadTileset("Cursor.bmp");          tsTree.loadTileset("treets.bmp");
42            tsCursor.loadTileset("Cursor.bmp");
43  }  }
44    
45  void initializeEngine() {  void initializeEngine() {
46          //load in the mousemap          //load in the mousemap
47          mouseMap.load("MouseMap.bmp");          mouseMap.create(tsBack.getTileList()[0].rcSrc.right - tsBack.getTileList()[0].rcSrc.left, tsBack.getTileList()[0].rcSrc.bottom - tsBack.getTileList()[0].rcSrc.top);
48    
49          //set up the tile plotter          //set up the tile plotter
         tilePlotter.setMapType(ISOMAP_SLIDE);  
50          tilePlotter.setTileSize(mouseMap.getWidth(), mouseMap.getHeight());          tilePlotter.setTileSize(mouseMap.getWidth(), mouseMap.getHeight());
51    
         //set up tile walker to diamond mode  
         tileWalker.setMapType(ISOMAP_SLIDE);  
   
52          //set up screeen space          //set up screeen space
53          RECT temp;          RECT temp;
54          setRect(&temp, 0, 0, 640, 480);          setRect(&temp, 0, 0, 640, 480);
55          scroller.setScreenSpace(&temp);          scroller.setScreenSpace(&temp);
56    
57          //grab tile extent from tileset          //grab tile extent from tileset
58          CopyRect(&temp, &tiles.getTileList()[0].rcDstExtent);          CopyRect(&temp, &tsBack.getTileList()[0].rcDstExtent);
59    
60          //calculate the worldspace          //calculate the worldspace
61          scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);          scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);
# Line 83  void initializeEngine() { Line 81  void initializeEngine() {
81          //set up the map to a random tilefield          //set up the map to a random tilefield
82          for(int r=0; r<MAP_HEIGHT; r++) {          for(int r=0; r<MAP_HEIGHT; r++) {
83                  for(int c=0; c<MAP_WIDTH; c++) {                  for(int c=0; c<MAP_WIDTH; c++) {
84                          theMap[r][c] = rand() % tiles.getTileCount();                          theMap[r][c] = rand() % 2;
85                  }                  }
86          }          }
87    
88            //calculate the extent rect
89            RECT extentRect;
90            CopyRect(&extentRect,&tsBack.getTileList()[0].rcDstExtent);
91            UnionRect(&extentRect, &extentRect, &tsShadow.getTileList()[0].rcDstExtent);
92            UnionRect(&extentRect, &extentRect, &tsTree.getTileList()[0].rcDstExtent);
93    
94            //set up the renderer
95            renderer.setBackBuffer(screen);
96            renderer.setExtentRect(&extentRect);
97            renderer.setFrameBuffer(frame);
98            renderer.setMapSize(MAP_WIDTH, MAP_HEIGHT);
99            renderer.setMouseMap(&mouseMap);
100            renderer.setPlotter(&tilePlotter);
101            renderer.setRenderFunction(RenderFunc);
102            renderer.setScroller(&scroller);
103            renderer.setUpdateRectCount(100);
104            renderer.setWalker(&tileWalker);
105    
106            //update the entire screenspace
107            renderer.addRect(scroller.getScreenSpace());
108    
109            //set up the cursor
110            cursorCoords.x = 0;
111            cursorCoords.y = 0;
112    }
113    
114    void RenderFunc(SDL_Surface* dst, RECT* clipRect, int xDst, int yDst, int xMap, int yMap) {
115            //put background tile
116            tsBack.clipTile(dst, clipRect, xDst, yDst, 0);
117            //check for a tree
118            if(theMap[xMap][yMap])
119            {
120                    //put shadow
121                    tsShadow.clipTile(dst, clipRect, xDst, yDst, 0);
122                    //put tree
123                    tsTree.clipTile(dst, clipRect, xDst, yDst, 0);
124            }
125  }  }
126    
127  void programLoop() {  void programLoop() {
128          bool done = false;          bool done = false;
129          SDL_Event event;          SDL_Event event;
130          Uint8 *keystate;          Uint8 *keystate;
131            POINT mousePoint;
132            int tempX, tempY;
133          while (!done) {          while (!done) {
134                  if(SDL_PollEvent(&event)) {                  if(SDL_GetMouseState(&tempX, &tempY)&SDL_BUTTON(1)) {
135                          switch (event.type) {                          click = true;
136            case SDL_KEYDOWN:                  }
137                    keystate = SDL_GetKeyState(NULL);                  mousePoint.x = tempX;
138                    if(keystate[SDLK_UP]) {                  mousePoint.y = tempY;
139                            scroller.moveAnchor(0, -10);                  cursorCoords = mouseMap.mapMouse(mousePoint);
140                    } else if(keystate[SDLK_DOWN]) {  
141                            scroller.moveAnchor(0, 10);                  //Make sure the mouse cursor is on the map:
142                    } else if(keystate[SDLK_LEFT]) {                  if(cursorCoords.x < 0) {
143                            scroller.moveAnchor(-10, 0);                          cursorCoords.x=0;
144                    } else if(keystate[SDLK_RIGHT]) {                  }
145                            scroller.moveAnchor(10, 0);                  if(cursorCoords.y < 0) {
146                    } else if(keystate[SDLK_1]) {                          cursorCoords.y=0;
147                            //Set the map type to diamond:                  }
148                            tilePlotter.setMapType(ISOMAP_DIAMOND);                  if(cursorCoords.x > (MAP_WIDTH-1)) {
149                            tileWalker.setMapType(ISOMAP_DIAMOND);                          cursorCoords.x=MAP_WIDTH-1;
150                    }
151                            //recalculate the scroller                  if(cursorCoords.y > (MAP_HEIGHT-1)) {
152                            scroller.calcWorldSpace(&tilePlotter, &tiles.getTileList()[0].rcDstExtent, MAP_WIDTH, MAP_HEIGHT);                          cursorCoords.y=MAP_HEIGHT-1;
153                            scroller.calcAnchorSpace();                  }
   
                           //set the screen anchor back to zero  
                           scroller.getAnchor()->x = 0;  
                           scroller.getAnchor()->y = 0;  
   
                           if(overheadView) {  
                                   mouseMap.load("MouseMap.bmp");  
   
                                   RECT temp;  
                                   setRect(&temp, 0, 0, 640, 480);  
   
                                   //Have to recalculate the tile sizes, too:  
                                   tilePlotter.setTileSize(mouseMap.getWidth(), mouseMap.getHeight());  
   
                                   //calculate the worldspace  
                                   scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);  
   
                                   //calculate anchor space  
                                   scroller.calcAnchorSpace();  
   
                                   //calculate the mousemap reference point  
                                   mouseMap.calculateReferencePoint(&tilePlotter, &temp);  
   
                                   overheadView = false;  
                           }  
                   } else if(keystate[SDLK_2]) {  
                           //Set the map type to slide:  
                           tilePlotter.setMapType(ISOMAP_SLIDE);  
                           tileWalker.setMapType(ISOMAP_SLIDE);  
   
                           //recalculate the scroller  
                           scroller.calcWorldSpace(&tilePlotter, &tiles.getTileList()[0].rcDstExtent, MAP_WIDTH, MAP_HEIGHT);  
                           scroller.calcAnchorSpace();  
   
                           //set the screen anchor back to zero  
                           scroller.getAnchor()->x = 0;  
                           scroller.getAnchor()->y = 0;  
   
                           if(overheadView) {  
                                   mouseMap.load("MouseMap.bmp");  
   
                                   RECT temp;  
                                   setRect(&temp, 0, 0, 640, 480);  
   
                                   //Have to recalculate the tile sizes, too:  
                                   tilePlotter.setTileSize(mouseMap.getWidth(), mouseMap.getHeight());  
   
                                   //calculate the worldspace  
                                   scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);  
   
                                   //calculate anchor space  
                                   scroller.calcAnchorSpace();  
   
                                   //calculate the mousemap reference point  
                                   mouseMap.calculateReferencePoint(&tilePlotter, &temp);  
   
                                   overheadView = false;  
                           }  
                   } else if(keystate[SDLK_3]) {  
                           //Set the map type to staggered:  
                           tilePlotter.setMapType(ISOMAP_STAGGERED);  
                           tileWalker.setMapType(ISOMAP_STAGGERED);  
   
                           //recalculate the scroller  
                           scroller.calcWorldSpace(&tilePlotter, &tiles.getTileList()[0].rcDstExtent, MAP_WIDTH, MAP_HEIGHT);  
                           scroller.calcAnchorSpace();  
   
                           //set the screen anchor back to zero  
                           scroller.getAnchor()->x = 0;  
                           scroller.getAnchor()->y = 0;  
   
                           if(overheadView) {  
                                   mouseMap.load("MouseMap.bmp");  
   
                                   RECT temp;  
                                   setRect(&temp, 0, 0, 640, 480);  
   
                                   //Have to recalculate the tile sizes, too:  
                                   tilePlotter.setTileSize(mouseMap.getWidth(), mouseMap.getHeight());  
   
                                   //calculate the worldspace  
                                   scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);  
   
                                   //calculate anchor space  
                                   scroller.calcAnchorSpace();  
   
                                   //calculate the mousemap reference point  
                                   mouseMap.calculateReferencePoint(&tilePlotter, &temp);  
   
                                   overheadView = false;  
                           }  
                   } else if(keystate[SDLK_4]) {  
                           //Set the map type to rectangular:  
                           tilePlotter.setMapType(ISOMAP_RECTANGULAR);  
                           tileWalker.setMapType(ISOMAP_RECTANGULAR);  
   
                           //recalculate the scroller  
                           scroller.calcWorldSpace(&tilePlotter, &squareTiles.getTileList()[0].rcDstExtent, MAP_WIDTH, MAP_HEIGHT);  
                           scroller.calcAnchorSpace();  
154    
155                            //set the screen anchor back to zero                  //scroll the map
156                            scroller.getAnchor()->x = 0;                  renderer.scrollFrame(scrollSpeed.x, scrollSpeed.y);
                           scroller.getAnchor()->y = 0;  
157    
158                            if(!overheadView) {                  //if a click was registered, add an update tile
159                                    mouseMap.create(squareTiles.getTileList()[0].rcSrc.right - squareTiles.getTileList()[0].rcSrc.left, squareTiles.getTileList()[0].rcSrc.bottom - squareTiles.getTileList()[0].rcSrc.top);                  if(click)
160                    {
161                            renderer.addTile(cursorCoords.x, cursorCoords.y);
162                            theMap[cursorCoords.x][cursorCoords.y] = 1 - theMap[cursorCoords.x][cursorCoords.y];
163                    }
164    
165                                    RECT temp;                  //set click to false
166                                    setRect(&temp, 0, 0, 640, 480);                  click = false;
167    
168                                    //Have to recalculate the tile sizes, too:                  //update the frame
169                                    tilePlotter.setTileSize(squareTiles.getTileList()[0].rcSrc.right - squareTiles.getTileList()[0].rcSrc.left, squareTiles.getTileList()[0].rcSrc.bottom - squareTiles.getTileList()[0].rcSrc.top);                  renderer.updateFrame();
170    
171                                    //calculate the worldspace                  //plot the cursor
172                                    scroller.calcWorldSpace(&tilePlotter, &temp, MAP_WIDTH, MAP_HEIGHT);                  POINT plotPoint;
173                    plotPoint = tilePlotter.plotTile(cursorCoords);
174                    plotPoint = scroller.WorldToScreen(plotPoint);
175                    tsCursor.clipTile(screen, scroller.getScreenSpace(), plotPoint.x, plotPoint.y, 0);
176    
177                                    //calculate anchor space                  //flip to show the back buffer
178                                    scroller.calcAnchorSpace();                  SDL_Flip(screen);
179    
180                                    //calculate the mousemap reference point                  scrollSpeed.x = 0;
181                                    mouseMap.calculateReferencePoint(&tilePlotter, &temp);                  scrollSpeed.y = 0;
182    
183                                    overheadView = true;                  if(SDL_PollEvent(&event)) {
184                            }                          switch (event.type) {
185                    }            case SDL_KEYDOWN:
186                    else if(keystate[SDLK_ESCAPE]) {                    keystate = SDL_GetKeyState(NULL);
187                            //done                    if(keystate[SDLK_UP]) {
188                            done = true;                            scrollSpeed.y = -100;
189                            break;                    } else if(keystate[SDLK_DOWN]) {
190                              scrollSpeed.y = 100;
191                      } else if(keystate[SDLK_LEFT]) {
192                              scrollSpeed.x = -100;
193                      } else if(keystate[SDLK_RIGHT]) {
194                              scrollSpeed.x = 100;
195                    }                    }
196                    break;                    break;
197            case SDL_QUIT:            case SDL_QUIT:
# Line 249  void programLoop() { Line 199  void programLoop() {
199                    break;                    break;
200                          }                          }
201                  }                  }
   
                 //Paint screen:  
                 renderMap();  
202          }          }
203  }  }
204    
 void renderMap() {  
         //Clean up back buffer:  
         SDL_FillRect(screen, NULL, 0);  
   
         //scroll the map depending on scroll speed:  
         scroller.moveAnchor(scrollSpeed.x, scrollSpeed.y);  
   
         //Plot tiles:  
         POINT plotPoint;  
         POINT mapPoint;  
         for(mapPoint.y=0; mapPoint.y<MAP_HEIGHT; mapPoint.y++) {  
                 for(mapPoint.x=0; mapPoint.x<MAP_WIDTH; mapPoint.x++) {  
                         //Find the world plot coordinates:  
                         plotPoint = tilePlotter.plotTile(mapPoint);  
   
                         //Convert to screen coordinates:  
                         plotPoint = scroller.WorldToScreen(plotPoint);  
   
                         //Plot tile:  
                         if(overheadView) {  
                                 squareTiles.putTile(screen, plotPoint.x, plotPoint.y, theMap[mapPoint.y][mapPoint.x]%2);  
                         } else {  
                                 tiles.putTile(screen, plotPoint.x, plotPoint.y, theMap[mapPoint.y][mapPoint.x]);  
                         }  
                 }  
         }  
   
         //Grab the mouse position to plot the cursor:  
         POINT mousePoint;  
         int tempX, tempY;  
         SDL_GetMouseState(&tempX, &tempY);  
         mousePoint.x = tempX;  
         mousePoint.y = tempY;  
   
         //Map mouse:  
         cursorCoords = mouseMap.mapMouse(mousePoint);  
   
         //Make sure the mouse cursor is on the map:  
         if(cursorCoords.x < 0) {  
                 cursorCoords.x=0;  
         }  
         if(cursorCoords.y < 0) {  
                 cursorCoords.y=0;  
         }  
         if(cursorCoords.x > (MAP_WIDTH-1)) {  
                 cursorCoords.x=MAP_WIDTH-1;  
         }  
         if(cursorCoords.y > (MAP_HEIGHT-1)) {  
                 cursorCoords.y=MAP_HEIGHT-1;  
         }  
   
         //Find the world coordinates of the cursor:  
         plotPoint = tilePlotter.plotTile(cursorCoords);  
   
         //Convert to screen coordinates to account for scrolling:  
         plotPoint = scroller.WorldToScreen(plotPoint);  
   
         //Plot cursor:  
         cursor.putTile(screen, plotPoint.x, plotPoint.y, 0);  
   
         //Flip the back buffer onto the screen:  
         SDL_Flip(screen);  
 }  
   
205  void testEngine() {  void testEngine() {
206    
207          /* Seed the random-number generator with current time so that          /* Seed the random-number generator with current time so that
208           * the map will be different every time we run. */          * the map will be different every time we run. */
209          srand((unsigned)time(NULL));          srand((unsigned)time(NULL));
210    
211          loadTileSets();          loadTileSets();
# Line 331  void testEngine() { Line 214  void testEngine() {
214          programLoop();          programLoop();
215  }  }
216    
217    void cleanUp() {
218            SDL_FreeSurface(frame);
219    }
220    
221  int main(int argc, char* args[]) {  int main(int argc, char* args[]) {
222          /* Initialize SDL */          /* Initialize SDL */
223          if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {          if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
# Line 339  int main(int argc, char* args[]) { Line 226  int main(int argc, char* args[]) {
226          }          }
227    
228          screen = createScreen(640, 480, "Isometric Engine Test", SDL_HWSURFACE | SDL_DOUBLEBUF);          screen = createScreen(640, 480, "Isometric Engine Test", SDL_HWSURFACE | SDL_DOUBLEBUF);
229            frame = createSurface(640, 480, SDL_HWSURFACE);
230    
231          testEngine();          testEngine();
232    
233            cleanUp();
234    
235          SDL_Quit();          SDL_Quit();
236    
237          return 0;          return 0;

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26