NoPaste []

[ Create new paste | Recent pastes ]

[ Show as plain text ]

Description: No description, Author: PG, Time: 2009-08-23 11:22, Language: C
http://nopaste.gamedev.pl?&id=4289
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <allegro.h>
#include <stdlib.h>
#include <math.h>

#define BUFFSIZE 20
#define WINDOWHEIGHT 480
#define WINDOWWIDTH 640
#define SCALEDETAIL 10

#define CBLACK       makecol (  0,   0,   0)
#define CBALL        makecol (255, 255, 255)
#define CINDICATOR   makecol ( 50, 255,   0)
#define CAUTHOR      makecol ( 50, 255,   0)
#define CPAUSE       makecol (255, 255,   0)
#define CSCALE       makecol (255, 255, 255)
#define CSCALE2      makecol (180, 180, 180)
#define CSCALENUM    makecol (255, 255, 255)
#define CSCALENUM2   makecol (180, 180, 180)
#define CGROUND      makecol (255, 255, 255)
#define CSIMINFO     makecol (255, 255, 255)
#define CMENUINFO    makecol (255, 255, 255)
#define CWHITE       makecol (255, 255, 255)

static short int nLeftMargin   = 20,               \
                 nRightMargin  = 20,               \
                 nTopMargin    = 20,               \
                 nBottomMargin = 20,               \
                 nMargin       = 2,                \
                 nFontHeight   = 8,                \             
                 nFontWidth    = 8,                \
                 nLineInterval = 5,                \
                 nScaleWidth   = 10,               \
                 nScaleMargin  = 100,              \
                 nCircleRadius = 10,               \
                 xCirclePos    = WINDOWWIDTH / 2 ;

char szPauseText[]         = "PAUSE",                                           \
     szAuthorText[]        = "Author: Pawel Guz",                               \
     szInfoText[]          = "\"S\" to start a new simulation, ESC to exit.",   \
     szAltitudeText[]      = "Altitude:",                                       \
     szAccelerationText[]  = "Acceleration:",                                   \
     szMemoryError[]       = "There is not enough memory to create a bitmap!\n" ;

BITMAP *bScreen = NULL;
void Draw (float fAltitude, float fAcceleration, float fScaleStep, float fTime) ;
void GetParameters (float* fAltitude, float* fAcceleration) ;
char* GetNumber (int xPosition, int yPosition) ;

void Initiate() ;
void Deinitiate() ;

volatile int msCounter = 0 ;
void Timer()
{
    msCounter++ ;
}
END_OF_FUNCTION (Timer)
     
int main()
{
    Initiate();
   
    float fAltitude, fAcceleration, fTime, fScaleStep;
     
    while(1)
    {
        clear_bitmap(bScreen);   
        textprintf(bScreen, font, nLeftMargin, nTopMargin, CAUTHOR, szAuthorText) ;
        textprintf(bScreen, font, nLeftMargin, nTopMargin + nFontHeight + nLineInterval, CMENUINFO, szInfoText) ;
        blit(bScreen, screen, 0, 0, 0, 0, WINDOWWIDTH, WINDOWHEIGHT) ;
               
        while(1)
        {
            while (!keypressed()) ;
            if (key[KEY_S])
            {
                readkey() ;
                GetParameters (&fAltitude, &fAcceleration) ;
                fScaleStep = fAltitude * SCALEDETAIL / (WINDOWHEIGHT - (nTopMargin + nBottomMargin)) ;
                break ;             
            }
            else if (key[KEY_ESC])
                Deinitiate() ;
        }   
       
        msCounter = 0 ;
        while (msCounter <= (sqrt (2 * fAltitude / fAcceleration) * 1000))
        {
            Draw (fAltitude, fAcceleration, msCounter * 0.001, fScaleStep)
           
            if(keypressed())
            {
                if (key[KEY_P])
                {
                    readkey();
                    fTime = msCounter;
                   
                    text_mode (CBLACK) ;
                    textout(screen, font, szPauseText, WINDOWWIDTH - nRightMargin - nFontWidth * (sizeof(szPauseText) / sizeof(char) - 1), nTopMargin, CPAUSE);
                    text_mode(-1);
                   
                    while (1)
                    {
                        while (!keypressed());
                        if (key[KEY_P] || key[KEY_ESC])
                        {
                            readkey() ;
                            break ;
                        }
                    }
                    msCounter = fTime;
                }
                else if (key[KEY_ENTER])
                {
                    readkey();
                    break
                }
                else if (key[KEY_ESC])
                {
                    readkey();
                    break
                }                 
            }       
        }
       
        Draw (fAltitude , fAcceleration, sqrt (2 * fAltitude / fAcceleration), fScaleStep);
        readkey();
    }
}
END_OF_MAIN()

void Draw (float fAltitude, float fAcceleration, float fTime, float fScaleStep)
{
    short int nCurrentStep ;
    int yCurrent, cyDisplacement ;
    float fDisplacement, fVelocity ;
               
       
    clear_bitmap(bScreen) ;
       
    vline(bScreen, nScaleMargin, nTopMargin, WINDOWHEIGHT - nTopMargin, CSCALE) ;
    for (nCurrentStep = 0, yCurrent = WINDOWHEIGHT - nBottomMargin ; yCurrent >= nTopMargin ; yCurrent -= SCALEDETAIL, nCurrentStep++ )
    {
        if (nCurrentStep % 2 == 0)
        {
            textprintf(bScreen, font, nLeftMargin, yCurrent , CSCALENUM, "%0.3f", nCurrentStep * fScaleStep);       
            hline(bScreen, nScaleMargin - nScaleWidth, yCurrent, nScaleMargin + nScaleWidth, CSCALE)
        }
        else if (nCurrentStep % 2 == 1)
        {
            textprintf(bScreen, font, nLeftMargin, yCurrent , CSCALENUM2, "%0.3f", nCurrentStep * fScaleStep);       
            hline(bScreen, nScaleMargin - nScaleWidth, yCurrent, nScaleMargin + nScaleWidth, CSCALE2)
        }     
    }
    hline(bScreen, nScaleMargin, WINDOWHEIGHT - nBottomMargin, WINDOWWIDTH - nRightMargin, CGROUND) ;
   
    fDisplacement = fAcceleration * fTime * fTime / 2 ;
    cyDisplacement = fDisplacement / fAltitude * (WINDOWHEIGHT - (nTopMargin + nBottomMargin)) + nTopMargin;
    fVelocity = fAcceleration * fTime;                                                                                   
   
    circle(bScreen, xCirclePos, cyDisplacement, nCircleRadius, CBALL) ;
    hline(bScreen, nScaleMargin, cyDisplacement, xCirclePos - nCircleRadius, CINDICATOR) ;
    textprintf(bScreen, font,  xCirclePos + 2 * nCircleRadius, cyDisplacement - nFontHeight, CSIMINFO, "v=%5.3f h=%0.3f t=%0.3f", fVelocity, (fAltitude - fDisplacement), fTime);

   
    blit(bScreen, screen, 0, 0, 0, 0, WINDOWWIDTH, WINDOWHEIGHT);   
    return
}

void GetParameters (float* fAltitude, float* fAcceleration)
{
    clear_bitmap(screen);   
   
    textprintf(screen, font, nLeftMargin, nTopMargin, CWHITE, szAltitudeText) ;
    while(1)
    {
        if ((*fAltitude = atof (GetNumber (nMargin + nLeftMargin + nFontWidth * (sizeof(szAltitudeText) / sizeof(char) - 1), nTopMargin))) == 0)
            continue ;
        else break ;
    }   
   
    textprintf(screen, font, nLeftMargin, nTopMargin + nFontHeight + nLineInterval, CWHITE, szAccelerationText) ;
    while(1)
    {
        if ((*fAcceleration = atof (GetNumber (nMargin + nLeftMargin + nFontWidth * (sizeof(szAccelerationText) / sizeof(char) - 1), nTopMargin + nFontHeight + nLineInterval))) == 0)
            continue ;
        else break ;
    }   
   
    readkey();
    return;   
}

char* GetNumber (int xPosition, int yPosition)
{
    BITMAP* TextBox ;
    char szBuffer[BUFFSIZE], cChar ;
    short int nCaret = 0;
    szBuffer[0] = '\0' ;
   
    if (!(TextBox = create_bitmap (nFontWidth * BUFFSIZE, nFontHeight)))
    {
        set_gfx_mode (GFX_TEXT, 0, 0, 0, 0);
        allegro_message (szMemoryError) ;
        allegro_exit();
        exit(1);
    }
   
    do
    {                         
        if (keypressed())
        {
            cChar = readkey() ;
                       
            if (key[KEY_BACKSPACE])
            {
                if (nCaret > 0)
                    nCaret-- ;
                szBuffer[nCaret] = '\0' ;   
            }                   
            else if (cChar >= '0' && cChar <= '9' || cChar == '.')
            {
                if (nCaret <= BUFFSIZE - 1)
                {
                    szBuffer[nCaret] = cChar ;   
                    nCaret++ ;
                    szBuffer[nCaret] = '\0' ;   
                }
            }
        }
       
        clear_bitmap (TextBox) ;     
        textout (TextBox, font, szBuffer, 0, 0, CWHITE) ;
        blit (TextBox, screen, 0, 0, xPosition, yPosition, TextBox->w, TextBox->h) ;
    }   
    while (!key[KEY_ENTER]) ;
   
    return szBuffer ;     
}

void Initiate()
{   
    allegro_init() ;
    install_keyboard() ;
    set_color_depth(32) ;
   
    set_gfx_mode (GFX_AUTODETECT_WINDOWED, WINDOWWIDTH, WINDOWHEIGHT, 0, 0);

    if (!(bScreen = create_bitmap (WINDOWWIDTH, WINDOWHEIGHT)))
    {
        set_gfx_mode (GFX_TEXT, 0, 0, 0, 0) ;
        allegro_message (szMemoryError) ;
        allegro_exit() ;
        exit(1) ;
    }

    clear_to_color (bScreen, CBLACK) ;

    LOCK_VARIABLE (msCounter) ;
    LOCK_FUNCTION (Timer) ;
    install_int (Timer, 1) ;
   
    return ;
}

void Deinitiate()
{
    readkey() ;
    destroy_bitmap (bScreen);
    allegro_exit();
    exit(1);
}

en pl 
Copyright © 2007 ayufan @ Warsztat
Based on GeSHi