/[bontz]/bontz/bontz.cpp
ViewVC logotype

Diff of /bontz/bontz.cpp

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

revision 1.7 by rickac, Wed Jun 18 00:54:11 2003 UTC revision 1.8 by swerk, Mon Aug 4 18:50:15 2003 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright 2002, 2003 Charles Rickard, Owen Swerkstrom   * Copyright 2002, 2003 Charles Rickard, Owen Swerkstrom
3   *   *
4   * This file is part of Bontz.              http://savannah.nongnu.org/bontz   * This file is part of Bontz.              http://savannah.nongnu.org/bontz
5   *   *
6   * Bontz is free software; you can redistribute it and/or modify   * Bontz is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * Bontz is distributed in the hope that it will be useful and fun,   * Bontz is distributed in the hope that it will be useful and fun,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with Bontz; if not, write to the Free Software   * along with Bontz; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */   */
20    
21  /*  /*
22    based on:    based on:
23    
24    lesson3.cpp    lesson3.cpp
25    Cone3D GFX with SDL lesson 3.    Cone3D GFX with SDL lesson 3.
26    Made by Marius Andra 2001    Made by Marius Andra 2001
27    http://cone3d.gamedev.net    http://cone3d.gamedev.net
28    
29    You can use the code for anything you like.    You can use the code for anything you like.
30    Even in a commercial project.    Even in a commercial project.
31    But please let me know where it ends up.    But please let me know where it ends up.
32    I'm just curious. That's all.    I'm just curious. That's all.
33  */  */
34    
35  /**  /**
36   *KEY:   *KEY:
37   * //~- not done   * //~- not done
38   * //~+ done   * //~+ done
39   * //~? misc notes   * //~? misc notes
40   *   *
41   *TODO:   *TODO:
42   * //~- need to write a memory manager   * //~- need to write a memory manager
43   **/   **/
44    
45  #include <stdio.h>  #include <stdio.h>
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <stdarg.h>  #include <stdarg.h>
48    
49  #include "lib/CSprite.h"  #include "lib/CSprite.h"
50  #include "lib/CSpriteBase.h"  #include "lib/CSpriteBase.h"
51    
52  #include "SDL/SDL.h"  #include "SDL/SDL.h"
53  #include "SDL/SDL_mixer.h"  #include "SDL/SDL_mixer.h"
54  #include "SDL/SDL_image.h"  #include "SDL/SDL_image.h"
55    
56  SDL_Surface *screen, *back, *dialog;  SDL_Surface *screen, *back, *dialog;
57  CSpriteBase muckabase;  //CSpriteBase muckabase;
58  CSprite mucka1;  //CSprite mucka1;
59  CSpriteBase fishbase1;  CSpriteBase fishbase1;
60  CSprite fish1;  CSprite fish1;
61  CSpriteBase fishbase2;  CSpriteBase fishbase2;
62  CSprite fish2;  CSprite fish2;
63  CSpriteBase fishbase3;  CSpriteBase fishbase3;
64  CSprite fish3;  CSprite fish3;
65  CSpriteBase fishbase4;  CSpriteBase fishbase4;
66  CSprite fish4;  CSprite fish4;
67  Mix_Music *music;  
68    CSprite bellguy;
69  int an_direction[3][3];  CSpriteBase bellguybase;
70    
71    CSprite furgason;
72  SDL_Surface * ImageLoad(char *file)  CSpriteBase furgasonbase;
73  {  CSprite textbox;
74    SDL_Surface *temp1, *temp2;  CSpriteBase textboxbase;
75    temp1 = SDL_LoadBMP(file);  
76    temp2 = SDL_DisplayFormat(temp1);  Mix_Music *music;
77    SDL_FreeSurface(temp1);  
78    return temp2;  int an_direction[3][3];
79  }  
80    
81  int InitImages()  SDL_Surface * ImageLoad(char *file)
82  {  {
83    //  SDL_Surface *temp1;    SDL_Surface *temp1, *temp2;
84      temp1 = SDL_LoadBMP(file);
85    //  dialog = SDL_DisplayFormat(IMG_Load("objects/dialog.png"));    temp2 = SDL_DisplayFormat(temp1);
86    back = ImageLoad("media/tile/misc/bg.bmp");    SDL_FreeSurface(temp1);
87    return 0;    return temp2;
88  }  }
89    
90  void DrawIMG(SDL_Surface *img, int x, int y)  int InitImages()
91  {  {
92    SDL_Rect dest;    //  SDL_Surface *temp1;
93    dest.x = x;  
94    dest.y = y;    //  dialog = SDL_DisplayFormat(IMG_Load("objects/dialog.png"));
95    SDL_BlitSurface(img, NULL, screen, &dest);    back = ImageLoad("media/tile/misc/bg.bmp");
96  }    return 0;
97    }
98  void DrawDialog(int x, int y){  
99    SDL_Rect dest;  void DrawIMG(SDL_Surface *img, int x, int y)
100    dest.x = x;  {
101    dest.y = y;    SDL_Rect dest;
102    SDL_BlitSurface(dialog, NULL, screen, &dest);    dest.x = x;
103  }    dest.y = y;
104      SDL_BlitSurface(img, NULL, screen, &dest);
105  void DrawBG()  }
106  {  
107    DrawIMG(back, 0, 0);  void DrawDialog(int x, int y){
108    //  DrawDialog(0,0);    SDL_Rect dest;
109  }    dest.x = x;
110      dest.y = y;
111  void DrawScene()    SDL_BlitSurface(dialog, NULL, screen, &dest);
112  {  }
113    
114    /***************  void DrawBG()
115    //Rickac:  {
116    //need to break this into multiple levels    DrawIMG(back, 0, 0);
117    //background    //  DrawDialog(0, 0);
118    //character  }
119    //menu  
120    void DrawScene()
121    //the draw method on the CSprite class should take an SDL_Surface variable as an argument  {
122    //the sprite will then be drawn on that surface.  
123    ***************/    /***************
124    //DrawBG();    //Rickac:
125    /*comment these two lines out*/    //need to break this into multiple levels
126    mucka1.clearBG();    //background
127    mucka1.updateBG();    //character
128    /**/    //menu
129    
130    fish1.clearBG();    //the draw method on the CSprite class should take an SDL_Surface variable as an argument
131    fish1.updateBG();    //the sprite will then be drawn on that surface.
132    fish2.clearBG();    ***************/
133    fish2.updateBG();    //DrawBG();
134    fish3.clearBG();    /*comment these two lines out*/
135    fish3.updateBG();  //  mucka1.clearBG();
136    fish4.clearBG();  //  mucka1.updateBG();
137    fish4.updateBG();    /**/
138    
139    //replace the following with this    fish1.clearBG();
140    //  mucka1.draw(screen);    fish1.updateBG();
141    mucka1.draw();    fish2.clearBG();
142    fish1.draw();    fish2.updateBG();
143    fish2.draw();    fish3.clearBG();
144    fish3.draw();    fish3.updateBG();
145    fish4.draw();    fish4.clearBG();
146      fish4.updateBG();
147    SDL_Flip(screen);  
148  }    textbox.clearBG();
149      textbox.updateBG();
150  int main(int argc, char *argv[])  
151  {    bellguy.clearBG();
152    Uint8* keys;    bellguy.updateBG();
153    
154    an_direction[0][0]=0;    furgason.clearBG();
155    an_direction[1][0]=0;    furgason.updateBG();
156    an_direction[1][1]=1;  
157    an_direction[0][1]=2;    //replace the following with this
158    an_direction[2][1]=3;    //  mucka1.draw(screen);
159    an_direction[2][0]=4;    //mucka1.draw();
160    an_direction[2][2]=5;    fish1.draw();
161    an_direction[0][2]=6;    fish2.draw();
162    an_direction[1][2]=7;    fish3.draw();
163      fish4.draw();
164    bool b_music_init=true;  
165    if(Mix_OpenAudio(44100, AUDIO_S16, 2, 2048) < 0)    bellguy.draw();
166    {  
167      b_music_init=false;    furgason.draw();
168      printf("Warning: Couldn't set 44100 Hz 16-bit audio");    textbox.draw();
169      printf(" - Reason: %s\n", SDL_GetError());  
170    }    SDL_Flip(screen);
171      }
172    if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )  
173    {  int main(int argc, char *argv[])
174      printf("Unable to init SDL: %s\n", SDL_GetError());  {
175      exit(1);    Uint8* keys;
176    }  
177      an_direction[0][0]=0;
178    //see if we opened our audio channel    an_direction[1][0]=0;
179    if(b_music_init){    an_direction[1][1]=1;
180      printf("Loading Music\n");    an_direction[0][1]=2;
181      music = Mix_LoadMUS("media/music/tropical.ogg");    an_direction[2][1]=3;
182      an_direction[2][0]=4;
183      // then start playing it.    an_direction[2][2]=5;
184      printf("Playing Music\n");    an_direction[0][2]=6;
185      Mix_PlayMusic(music, 0);    an_direction[1][2]=7;
186    }  
187      bool b_music_init=true;
188    atexit(SDL_Quit);    if(Mix_OpenAudio(44100, AUDIO_S16, 2, 2048) < 0)
189      {
190    screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);      b_music_init=false;
191    if ( screen == NULL )      printf("Warning: Couldn't set 44100 Hz 16-bit audio");
192    {      printf(" - Reason: %s\n", SDL_GetError());
193      printf("Unable to set 640x480 video: %s\n", SDL_GetError());    }
194      exit(1);    
195    }    if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
196      {
197    int direction=0;      printf("Unable to init SDL: %s\n", SDL_GetError());
198    //this is used to determine whether the person is running or walking      exit(1);
199    int multiplier;    }
200    
201    printf("Loading Sprites\n");    //see if we opened our audio channel
202    muckabase.init("media/sprite/mucka");    if(b_music_init){
203    //fishbase1.init("media/sprite/flopfish/fatfish");      printf("Loading Music\n");
204    fishbase1.init("media/tile/outdoor/water");      music = Mix_LoadMUS("media/music/tropical.ogg");
205    fishbase2.init("media/sprite/flopfish/chunkyfish");  
206    //fishbase3.init("media/sprite/flopfish/longfish");      // then start playing it.
207          printf("Playing Music\n");
208    fishbase3.init("media/tile/outdoor/pole");      Mix_PlayMusic(music, 0);
209    fishbase4.init("media/sprite/flopfish/punyfish");    }
210    
211    fish1.init(&fishbase1,screen);    atexit(SDL_Quit);
212    fish1.set(40,300);  
213    fish1.setSpeed(1);    screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);
214    fish1.setTrack(0);    if ( screen == NULL )
215    fish1.startAnim();    {
216        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
217    fish2.init(&fishbase2,screen);      exit(1);
218    fish2.set(60,300);    }
219    fish2.setSpeed(1);  
220    fish2.setTrack(0);    int direction=0;
221    fish2.startAnim();    //this is used to determine whether the person is running or walking
222      int multiplier;
223    fish3.init(&fishbase3,screen);  
224    fish3.set(160,300);    printf("Loading Sprites\n");
225    fish3.setSpeed(1);  //  muckabase.init("media/sprite/hero/mucka");
226    fish3.setTrack(0);    //fishbase1.init("media/sprite/flopfish/fatfish");
227    fish3.startAnim();    fishbase1.init("media/tile/outdoor/water");
228      fishbase2.init("media/sprite/npc/flopfish/chunkyfish");
229    fish4.init(&fishbase4,screen);    //fishbase3.init("media/sprite/flopfish/longfish");
230    fish4.set(160,235);    
231    fish4.setSpeed(1);    fishbase3.init("media/tile/outdoor/pole");
232    fish4.setTrack(0);    fishbase4.init("media/sprite/npc/flopfish/punyfish");
233    fish4.startAnim();  
234      bellguybase.init("media/sprite/npc/bellguy");
235      bellguy.init(&bellguybase, screen);
236    mucka1.init(&muckabase,screen);    bellguy.set(500, 200);
237    mucka1.set(300,300);    bellguy.setSpeed(1);
238    mucka1.setSpeed(1);    bellguy.setTrack(0);
239    mucka1.setTrack(direction);    bellguy.startAnim();
240    mucka1.startAnim();  
241      furgasonbase.init("media/sprite/hero/furgason");
242    SDL_ShowCursor(0);    //furgason.init(&furgasonbase, screen);
243      //furgason.set(200, 200);
244    InitImages();    //furgason.setSpeed(1);
245    DrawBG();    //furgason.setTrack(0);
246      //furgason.startAnim();
247    int done=0;  
248      textboxbase.init("media/menu");
249    int LastUpdate;    textbox.init(&textboxbase, screen);
250    LastUpdate=SDL_GetTicks();    textbox.set(400, 300);
251    int GameSpeed=10;    textbox.setSpeed(1);
252    int PrintScreenTime=0;    textbox.setTrack(0);
253    while(done == 0)    textbox.startAnim();
254    {  
255      SDL_Event event;    fish1.init(&fishbase1,screen);
256          fish1.set(40,300);
257      fish1.setSpeed(1);
258      while ( SDL_PollEvent(&event) )    fish1.setTrack(0);
259      {    fish1.startAnim();
260        if ( event.type == SDL_QUIT )  {  done = 1;  }  
261      fish2.init(&fishbase2,screen);
262        if ( event.type == SDL_KEYDOWN )    fish2.set(60,300);
263        {    fish2.setSpeed(1);
264          if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }    fish2.setTrack(0);
265        }    fish2.startAnim();
266    
267        //rickac added code to print screen    fish3.init(&fishbase3,screen);
268        //this should eventually be changed so it adds a number to screen    fish3.set(160,300);
269        //so it doesn't always overwrite the same file    fish3.setSpeed(1);
270        //ex:  screen1.bmp screen2.bmp screen3.bmp  and so on    fish3.setTrack(0);
271        if( event.key.keysym.sym == SDLK_PRINT){    fish3.startAnim();
272          //delay the print screen longer than the normal game time, so we don't continually take screen shots  
273          if(PrintScreenTime+GameSpeed*100<SDL_GetTicks()){    fish4.init(&fishbase4,screen);
274            PrintScreenTime=SDL_GetTicks();    fish4.set(160,235);
275            SDL_SaveBMP(screen, "screen.bmp");    fish4.setSpeed(1);
276          }    fish4.setTrack(0);
277        }    fish4.startAnim();
278      }  
279      keys = SDL_GetKeyState(NULL);    furgason.init(&furgasonbase,screen);
280      int x,y;    furgason.set(300,300);
281      x=0;    furgason.setSpeed(1);
282      y=0;    furgason.setTrack(direction);
283      furgason.startAnim();
284      //walking is 1  
285      //running is 2    SDL_ShowCursor(0);
286      multiplier=1;  
287      //our timer to make sure we don't go too fast    InitImages();
288      if (LastUpdate+GameSpeed<SDL_GetTicks()){    DrawBG();
289        LastUpdate=SDL_GetTicks();  
290      int done=0;
291        if ( keys[SDLK_RSHIFT] ) { multiplier=2;}  
292        if ( keys[SDLK_LSHIFT] ) { multiplier=2;}    int LastUpdate;
293            LastUpdate=SDL_GetTicks();
294        if ( keys[SDLK_UP] ) { mucka1.yadd(-1*multiplier);y=2;}    int GameSpeed=10;
295        if ( keys[SDLK_DOWN] ) { mucka1.yadd(1*multiplier);y=1;}    int PrintScreenTime=0;
296        if ( keys[SDLK_LEFT] ) { mucka1.xadd(-1*multiplier);x=2;}    while(done == 0)
297        if ( keys[SDLK_RIGHT] ) { mucka1.xadd(1*multiplier);x=1;}    {
298        SDL_Event event;
299        if (!keys[SDLK_RIGHT] && !keys[SDLK_LEFT] && !keys[SDLK_DOWN] && !keys[SDLK_UP]){      
300          if(mucka1.getTrack()!=direction){  
301            mucka1.setTrack(direction);      while ( SDL_PollEvent(&event) )
302          }      {
303        }else{        if ( event.type == SDL_QUIT )  {  done = 1;  }
304          direction=an_direction[x][y];  
305          if(mucka1.getTrack()!=(direction+(8*multiplier))){        if ( event.type == SDL_KEYDOWN )
306            mucka1.setTrack(direction+(8*multiplier));        {
307          }          if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
308        }        }
309      }  
310          //rickac added code to print screen
311      DrawScene();        //this should eventually be changed so it adds a number to screen
312    }        //so it doesn't always overwrite the same file
313          //ex:  screen1.bmp screen2.bmp screen3.bmp  and so on
314    if(b_music_init){        if( event.key.keysym.sym == SDLK_PRINT){
315      // end the music in 1 second          //delay the print screen longer than the normal game time, so we don't continually take screen shots
316      Mix_FadeOutMusic(1000);          if(PrintScreenTime+GameSpeed*100<SDL_GetTicks()){
317      printf("Fadding Music\n");            PrintScreenTime=SDL_GetTicks();
318      Mix_FreeMusic(music);            SDL_SaveBMP(screen, "screen.bmp");
319    }          }
320    return 0;        }
321  }      }
322        keys = SDL_GetKeyState(NULL);
323        int x,y;
324        x=0;
325        y=0;
326    
327        //walking is 1
328        //running is 2
329        multiplier=1;
330        //our timer to make sure we don't go too fast
331        if (LastUpdate+GameSpeed<SDL_GetTicks()){
332          LastUpdate=SDL_GetTicks();
333    
334          if ( keys[SDLK_RSHIFT] ) { multiplier=2;}
335          if ( keys[SDLK_LSHIFT] ) { multiplier=2;}
336          
337          if ( keys[SDLK_UP] ) { furgason.yadd(-1*multiplier);y=2;}
338          if ( keys[SDLK_DOWN] ) { furgason.yadd(1*multiplier);y=1;}
339          if ( keys[SDLK_LEFT] ) { furgason.xadd(-1*multiplier);x=2;}
340          if ( keys[SDLK_RIGHT] ) { furgason.xadd(1*multiplier);x=1;}
341    
342          if (!keys[SDLK_RIGHT] && !keys[SDLK_LEFT] && !keys[SDLK_DOWN] && !keys[SDLK_UP]){
343            if(furgason.getTrack()!=direction){
344              furgason.setTrack(direction);
345            }
346          }else{
347            direction=an_direction[x][y];
348            if(furgason.getTrack()!=(direction+(8*multiplier))){
349              furgason.setTrack(direction+(8*multiplier));
350            }
351          }
352        }
353    
354        DrawScene();
355      }
356    
357      if(b_music_init){
358        // end the music in 1 second
359        Mix_FadeOutMusic(1000);
360        printf("Fading Music\n");
361        Mix_FreeMusic(music);
362      }
363      return 0;
364    }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

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