1 /* Copyright (C) 2002 W.P. van Paassen - peter@paassen.tmfweb.nl,
2                       Ed Sinjiashvili  - slimb@swes.saren.ru
3 		      dekoder          - dekoder81@users.sourceforge.net
4    Copyright (C) 2004 flobo for this hacked version
5 
6    This program is free software; you can redistribute it and/or modify it under
7    the terms of the GNU General Public License as published by the Free
8    Software Foundation; either version 2 of the License, or (at your
9    option) any later version.
10 
11    This program is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; see the file COPYING.  If not, write to the Free
18    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19 
20 /* There is still room for improvements... */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #include <string.h>
26 #include "PuyoDoomMelt.h"
27 
28 /* A few definitions */
29 
30 #define SCREEN_WIDTH 640
31 #define SCREEN_HEIGHT 480
32 #define COL_WIDTH 8
33 #define NUM_COLS SCREEN_WIDTH / COL_WIDTH + 1
34 
rand_fun()35 static int rand_fun () {
36     return rand() % 256;
37 }
38 
39 typedef struct column {
40     short x;
41     short y;
42 } column_t;
43 
44 /* Private methods */
45 
46 static void _init_columns (DoomMelt *_this);
47 static void _column_draw  (column_t *column, SDL_Surface *meltImage, SDL_Surface *screen);
48 static void _column_think (column_t *column, int *isFinished);
49 
50 
51 /* Private members */
52 
53 struct _DoomMelt {
54   column_t     columns[NUM_COLS];
55   IIM_Surface *surf;
56   int          isFinished;
57 };
58 
59 /* Public methods */
60 
doom_melt_new(void)61 DoomMelt *doom_melt_new (void)
62 {
63   DoomMelt *_this = (DoomMelt*)malloc(sizeof(DoomMelt));
64   _this->isFinished = 1;
65   return _this;
66 }
67 
doom_melt_delete(DoomMelt * _this)68 void doom_melt_delete (DoomMelt *_this)
69 {
70   free(_this);
71 }
72 
doom_melt_start(DoomMelt * _this,IIM_Surface * surf)73 void doom_melt_start (DoomMelt *_this, IIM_Surface *surf)
74 {
75   _this->surf       = surf;
76   _this->isFinished = 0;
77   _init_columns(_this);
78 }
79 
doom_melt_update(DoomMelt * _this)80 void doom_melt_update (DoomMelt *_this)
81 {
82   if (!_this->isFinished)
83   {
84     int i = 0;
85     _this->isFinished = 1;
86     for (; i < NUM_COLS; i++)
87       _column_think (&_this->columns[i], &_this->isFinished);
88   }
89 }
90 
doom_melt_display(DoomMelt * _this,SDL_Surface * display)91 void doom_melt_display(DoomMelt *_this, SDL_Surface *display)
92 {
93   if (!_this->isFinished)
94   {
95     int i = 0;
96     for (; i < NUM_COLS; i++)
97     _column_draw(&_this->columns[i], _this->surf->surf, display);
98   }
99 }
100 
101 /*
102  * Private methods
103  */
104 
_init_columns(DoomMelt * _this)105 void _init_columns (DoomMelt *_this)
106 {
107     int i, start_x = 1;
108 
109     _this->columns[0].y = -(rand_fun() % 16);
110     _this->columns[0].x = 0;
111 
112     for (i = 1; i < NUM_COLS; i++)
113     {
114         int r = (rand_fun() % 3) - 1;
115         _this->columns[i].y = _this->columns[i-1].y + r;
116         if (_this->columns[i].y > 0)
117             _this->columns[i].y = 0;
118         else if (_this->columns[i].y == -16)
119             _this->columns[i].y = -15;
120 
121         _this->columns[i].x = start_x;
122 
123         start_x += COL_WIDTH;
124     }
125 }
126 
_column_draw(column_t * column,SDL_Surface * meltImage,SDL_Surface * display)127 void _column_draw (column_t *column, SDL_Surface *meltImage, SDL_Surface *display)
128 {
129     static SDL_Rect image_rect = {0, 0, COL_WIDTH, };
130     static SDL_Rect dest_rect =  {0, 0, COL_WIDTH, SCREEN_HEIGHT};
131 
132     int tmp = column->y;
133     if (tmp < 0)
134         tmp = 0;
135 
136     dest_rect.x = column->x;
137     dest_rect.y = tmp;
138 
139     image_rect.x = column->x;
140     image_rect.h = meltImage->h - tmp;
141 
142     SDL_BlitSurface(meltImage, &image_rect, display, &dest_rect);
143 }
144 
_column_think(column_t * column,int * isFinished)145 void _column_think (column_t *column, int *isFinished)
146 {
147     static int grow = 0;
148 
149     if (column->y < 0)
150     {
151         *isFinished = 0;
152         grow = 1;
153     }
154     else if (column->y < SCREEN_HEIGHT)
155     {
156         *isFinished = 0;
157         if (column->y < 16)
158             grow = column->y+3;
159         else
160         {
161             grow = 15;
162         }
163     }
164 
165     column->y += grow;
166 }
167 
168