Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » allegro performance problems

This thread is locked; no one can reply to it. rss feed Print
allegro performance problems
malleus
Member #8,425
March 2007

I am using a hp dv6000t laptop with a base install of Debian linux. It have a 256mb Geforce go 7400 nvidia graphics card. When I run my programs they get awful framerate. I have run some allegro examples and they all seem to run perfectly. It seems that just my programs lag. Why do they lag? It would be helpful if you told me exactly what to do as I am new to allegro and linux in general. I read somewhere that
using DGA 2.0 drivers made it fast or something. I have tried that but it had a segmentation fault. Thanks for your help in advance.

relpatseht
Member #5,034
September 2004
avatar

On Linux, I don't think there is any hardware acceleration of Allegro programs, therefore, if you wish for your programs to perform better, the only advice I can give it to write better code. Look into dirty rectangles for updating the screen and make sure you only blit objects which will actually be displayed.

HoHo
Member #4,534
April 2004
avatar

How big is the framerate difference compared to windows?
What kind of bitmaps are you using (memory, system, video)?
Are you using lots of fancy effects (blending, trasparency)?

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Evert
Member #794
November 2000
avatar

Quote:

When I run my programs they get awful framerate. I have run some allegro examples and they all seem to run perfectly. It seems that just my programs lag. Why do they lag?

Post code. We can't tell what's different between your program and Allegro's examples without seeing them.

Quote:

It would be helpful if you told me exactly what to do as I am new to allegro and linux in general.

Post code. ;) We need to see what you're doing in order to be able to tell you if you're doing something wrong.

Quote:

I read somewhere that using DGA 2.0 drivers made it fast or something. I have tried that but it had a segmentation fault.

DGA 2 allows you to use hardware acceleration in X11, but it's officially deprecated (in X11, not Allegro itself). Note that the driver requires root privileges and will fail to initialise if it can't get them (meaning you get a segfault if you don't check the return value of set_gfx_mode).

malleus
Member #8,425
March 2007

As you can tell the following is a pong ripoff. Please excuse my global variables and other bad code. I noticed that it only lags when i draw shapes to the screen not bitmaps. On my computer it runs smoothly for three seconds and then lags sooo badly.

variables.h file

#SelectExpand
1BITMAP *opening; 2BITMAP *buffer; 3 4int temp; 5int a=0; 6 7 8int p1Score; 9int p2Score; 10//player one paddle// 11int xP1=20; 12int yP1=20; 13 14 15int tempXP1; 16int tempYP1; 17 18//player two paddle// 19int xP2=800; 20int yP2=360; 21 22int tempXP2; 23int tempYP2; 24 25//ball// 26int xBall=420; 27int yBall=460; 28 29int tempXBall; 30int tempYBall; 31//--------------------------// 32 33int dir=1; //1= up, left. 2= down, left. 3=up, right. 4=down, right.

misc.h

#SelectExpand
1void draw(); 2void player1(); 3void player2(); 4void ballMovement(); 5 6 7void draw() 8{ 9 acquire_screen(); 10 11 textprintf_ex(buffer, font, 0, 460, makecol(0, 0, 0), -1, "Player 1 Score: %d", (p1Score-1)); 12 textprintf_ex(buffer, font, 0, 460, makecol(255, 255, 255), -1, "Player 1 Score: %d", p1Score); 13 14 textprintf_ex(buffer, font, 700, 460, makecol(0, 0, 0), -1, "Player 2 Score: %d", (p2Score-1)); 15 textprintf_ex(buffer, font, 700, 460, makecol(255, 255, 255), -1, "Player 2 Score: %d", p2Score); 16 17 rectfill(buffer, tempXP1, tempYP1, (tempXP1+20), (tempYP1+100), makecol(0, 0, 0)); //clear 18 rect(buffer, xP1, yP1, (xP1+20), (yP1+100), makecol(255, 255, 255)); //player one paddle 19 20 rectfill(buffer, tempXP2, tempYP2, (tempXP2+20), (tempYP2+100), makecol(0, 0, 0)); //clear 21 rect(buffer, xP2, yP2, (xP2+20), (yP2+100), makecol(255, 255, 255)); //player two paddle 22 23 circlefill(buffer, tempXBall, tempYBall, 10, makecol(0, 0, 0)); 24 circle(buffer, xBall, yBall, 10, makecol(255, 255, 255)); 25 26 line(buffer, 0, 10, 840, 10, makecol(255, 255, 255)); 27 line(buffer, 0, 470, 840, 470, makecol(255, 255, 255)); 28 line(buffer, 420, 0, 420, 480, makecol(255, 255, 255)); 29 30 draw_sprite(screen, buffer, 0, 0); 31 32 a++; 33 if((a%3)==0) 34 rest(1); 35 36 release_screen(); 37} 38void player1() 39{ 40 clear_keybuf(); 41 42 if(key[KEY_W] && (yP1-2)!=10) 43 yP1-=2; 44 if(key[KEY_S] && (yP1+102)!=470) 45 yP1+=2; 46 if(key[KEY_D] && (xP1+22)!=420) 47 xP1+=2; 48 if(key[KEY_A] && (xP1-2)!=0) 49 xP1-=2; 50 51} 52void player2() 53{ 54 clear_keybuf(); 55 56 if(key[KEY_UP] && (yP2-2)!=10) 57 yP2-=2; 58 if(key[KEY_DOWN] && (yP2+102)!=470) 59 yP2+=2; 60 if(key[KEY_RIGHT] && (xP2+20)!=840) 61 xP2+=2; 62 if(key[KEY_LEFT] && (xP2-2)!=420) 63 xP2-=2; 64} 65void ballmovement() 66{ 67 switch(dir) 68 { 69 case 1: 70 if((yBall-10)==10) 71 dir=2; 72 else if((xBall-10)==(xP1+20)) 73 for( int c=yP1; c<(yP1+100);c++) 74 if((yBall-10)==c) 75 dir=3; 76 yBall-=2, xBall-=2; 77 break; 78 case 2: 79 if((yBall+10)==470) 80 dir=1; 81 else if((xBall-10)==(xP1+20)) 82 for( int c=yP1; c<(yP1+100);c++) 83 if((yBall-10)==c) 84 dir=4; 85 yBall+=2, xBall-=2; 86 break; 87 case 3: 88 if((yBall-10)==10) 89 dir=4; 90 else if((xBall+10)==xP2) 91 for( int c=yP2; c<(yP2+100);c++) 92 if((yBall+10)==c) 93 dir=1; 94 yBall-=2, xBall+=2; 95 break; 96 case 4: 97 if((yBall+10)==470) 98 dir=3; 99 else if((xBall+10)==xP2) 100 for( int c=yP2; c<(yP2+100);c++) 101 if((yBall+10)==c) 102 dir=2; 103 yBall+=2, xBall+=2; 104 break; 105 } 106}

main.cpp

#SelectExpand
1//Pong 2.0 2//by Sharif Mabie-Alkhaldi 3#include <allegro.h> 4#include"variables.h" 5#include "misc.h" 6 7int main() 8{ 9 allegro_init(); 10 install_keyboard(); 11 set_color_depth(16); 12 13 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 840, 480, 0, 0); 14 15 opening=load_bitmap("opening.bmp", 0); 16 buffer=create_bitmap(840, 480); 17 18 start: 19 20 p1Score=0; 21 p2Score=0; 22 23 acquire_screen(); 24 draw_sprite(buffer, opening, 0, 0); 25 draw_sprite(screen, buffer, 0, 0); 26 release_screen(); 27 28 readkey(); 29 acquire_screen(); 30 clear_to_color( buffer, makecol(0, 0, 0)); 31 release_screen(); 32 33 while(!key[KEY_ESC]) 34 { 35 tempXP1=xP1; 36 tempXP2=xP2; 37 38 tempYP1=yP1; 39 tempYP2=yP2; 40 41 tempXBall=xBall; 42 tempYBall=yBall; 43 44 ballmovement(); 45 46 player1(); 47 player2(); 48 49 if((xBall+10)==0) 50 { 51 p2Score++; 52 53 yBall=460; 54 xBall=420; 55 } 56 else if((xBall-10)==840) 57 { 58 p1Score++; 59 60 yBall=460; 61 xBall=420; 62 } 63 64 if(p1Score==5) 65 { 66 textout(screen, font, "Player one wins! Do you want to play again y/esc?", 250, 240, 50); 67 readkey(); 68 if(key[KEY_Y]) 69 goto start; 70 } 71 else if(p2Score==5) 72 { 73 textout(screen, font, "Player two wins! Do you want to play again y/esc?", 250, 240, 50); 74 readkey(); 75 if(key[KEY_Y]) 76 goto start; 77 } 78 draw(); 79 }while(!key[KEY_ESC]); 80 return 0; 81} 82END_OF_MAIN();

HoHo
Member #4,534
April 2004
avatar

Loose the acquire/release screen functions and use regular blit to copy buffer to screen.

IIRC, acquire screen was good only when you had to access the screen bitmap several times per frame. Blitting buffer to screen does not qualify for such a situation.

You don't seem to be clearing the buffer each frame. Do you always redraw the entire screen every frame? From the code it doesn't look like that.

Also you might want to learn about allegro timers. Using only rest(1) for timing will not work for anyone else but you.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

X-G
Member #856
December 2000
avatar

First off, get rid of all those acquire_screen()s. And as a side note, use blit instead of draw_sprite to copy the backbuffer to screen. Secondly, your desktop is likely running in 32-bit, so opening a 16-bit window may cause further slowdowns. On an unrelated but vital note, you don't seem to have any kind of timer logic.

EDIT: Oh, you think you're clever, do you HoHo? I'm gonna cut you. grumbles

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

malleus
Member #8,425
March 2007

It still lags. Good someone post code that runs at a decent rate on linux. Theres seems to be a bit of a increase in performance but nothing vary noticable.

HoHo
Member #4,534
April 2004
avatar

What's the FPS you are getting?

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

malleus
Member #8,425
March 2007

3-4 at best. Could some one post code?

X-G
Member #856
December 2000
avatar

That wouldn't help. What's your code look like now?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

malleus
Member #8,425
March 2007

Ok sorry, it did in fact increase performance considerably. But it still gets sub par performance. When I move the paddles its kinda of jumpy. Heres my code.

main.cpp

#SelectExpand
1//Pong 2.0 2//by Sharif Mabie-Alkhaldi 3#include <allegro.h> 4#include"variables.h" 5#include "misc.h" 6 7int main() 8{ 9 allegro_init(); 10 install_keyboard(); 11 set_color_depth(16); 12 13 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 840, 480, 0, 0); 14 15 opening=load_bitmap("opening.bmp", 0); 16 buffer=create_bitmap(840, 480); 17 18 start: 19 20 p1Score=0; 21 p2Score=0; 22 23 blit(opening, buffer, 0, 0, 0, 0, opening->w, opening->h); 24 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 25 26 readkey(); 27 clear_to_color( buffer, makecol(0, 0, 0)); 28 29 while(!key[KEY_ESC]) 30 { 31 tempXP1=xP1; 32 tempXP2=xP2; 33 34 tempYP1=yP1; 35 tempYP2=yP2; 36 37 tempXBall=xBall; 38 tempYBall=yBall; 39 40 ballmovement(); 41 42 player1(); 43 player2(); 44 45 if((xBall+10)==0) 46 { 47 p2Score++; 48 49 yBall=460; 50 xBall=420; 51 } 52 else if((xBall-10)==840) 53 { 54 p1Score++; 55 56 yBall=460; 57 xBall=420; 58 } 59 60 if(p1Score==5) 61 { 62 textout(screen, font, "Player one wins! Do you want to play again y/esc?", 250, 240, 50); 63 readkey(); 64 if(key[KEY_Y]) 65 goto start; 66 } 67 else if(p2Score==5) 68 { 69 textout(screen, font, "Player two wins! Do you want to play again y/esc?", 250, 240, 50); 70 readkey(); 71 if(key[KEY_Y]) 72 goto start; 73 } 74 draw(); 75 }while(!key[KEY_ESC]); 76 return 0; 77} 78 79 80END_OF_MAIN();

misc.h

#SelectExpand
1void draw(); 2void player1(); 3void player2(); 4void ballMovement(); 5 6 7void draw() 8{ 9 10 textprintf_ex(buffer, font, 0, 460, makecol(0, 0, 0), -1, "Player 1 Score: %d", (p1Score-1)); 11 textprintf_ex(buffer, font, 0, 460, makecol(255, 255, 255), -1, "Player 1 Score: %d", p1Score); 12 13 textprintf_ex(buffer, font, 700, 460, makecol(0, 0, 0), -1, "Player 2 Score: %d", (p2Score-1)); 14 textprintf_ex(buffer, font, 700, 460, makecol(255, 255, 255), -1, "Player 2 Score: %d", p2Score); 15 16 rectfill(buffer, tempXP1, tempYP1, (tempXP1+20), (tempYP1+100), makecol(0, 0, 0)); //clear 17 rect(buffer, xP1, yP1, (xP1+20), (yP1+100), makecol(255, 255, 255)); //player one paddle 18 19 rectfill(buffer, tempXP2, tempYP2, (tempXP2+20), (tempYP2+100), makecol(0, 0, 0)); //clear 20 rect(buffer, xP2, yP2, (xP2+20), (yP2+100), makecol(255, 255, 255)); //player two paddle 21 22 circlefill(buffer, tempXBall, tempYBall, 10, makecol(0, 0, 0)); 23 circle(buffer, xBall, yBall, 10, makecol(255, 255, 255)); 24 25 line(buffer, 0, 10, 840, 10, makecol(255, 255, 255)); 26 line(buffer, 0, 470, 840, 470, makecol(255, 255, 255)); 27 line(buffer, 420, 0, 420, 480, makecol(255, 255, 255)); 28 29 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 30 31 a++; 32 if((a%3)==0) 33 rest(1); 34} 35void player1() 36{ 37 clear_keybuf(); 38 39 if(key[KEY_W] && (yP1-2)!=10) 40 yP1-=2; 41 if(key[KEY_S] && (yP1+102)!=470) 42 yP1+=2; 43 if(key[KEY_D] && (xP1+22)!=420) 44 xP1+=2; 45 if(key[KEY_A] && (xP1-2)!=0) 46 xP1-=2; 47 48} 49void player2() 50{ 51 clear_keybuf(); 52 53 if(key[KEY_UP] && (yP2-2)!=10) 54 yP2-=2; 55 if(key[KEY_DOWN] && (yP2+102)!=470) 56 yP2+=2; 57 if(key[KEY_RIGHT] && (xP2+20)!=840) 58 xP2+=2; 59 if(key[KEY_LEFT] && (xP2-2)!=420) 60 xP2-=2; 61} 62void ballmovement() 63{ 64 switch(dir) 65 { 66 case 1: 67 if((yBall-10)==10) 68 dir=2; 69 else if((xBall-10)==(xP1+20)) 70 for( int c=yP1; c<(yP1+100);c++) 71 if((yBall-10)==c) 72 dir=3; 73 yBall-=2, xBall-=2; 74 break; 75 case 2: 76 if((yBall+10)==470) 77 dir=1; 78 else if((xBall-10)==(xP1+20)) 79 for( int c=yP1; c<(yP1+100);c++) 80 if((yBall-10)==c) 81 dir=4; 82 yBall+=2, xBall-=2; 83 break; 84 case 3: 85 if((yBall-10)==10) 86 dir=4; 87 else if((xBall+10)==xP2) 88 for( int c=yP2; c<(yP2+100);c++) 89 if((yBall+10)==c) 90 dir=1; 91 yBall-=2, xBall+=2; 92 break; 93 case 4: 94 if((yBall+10)==470) 95 dir=3; 96 else if((xBall+10)==xP2) 97 for( int c=yP2; c<(yP2+100);c++) 98 if((yBall+10)==c) 99 dir=2; 100 yBall+=2, xBall+=2; 101 break; 102 } 103}

variables.h

#SelectExpand
1BITMAP *opening; 2BITMAP *buffer; 3 4int temp; 5int a=0; 6 7 8int p1Score; 9int p2Score; 10//player one paddle// 11int xP1=20; 12int yP1=20; 13 14 15int tempXP1; 16int tempYP1; 17 18//player two paddle// 19int xP2=800; 20int yP2=360; 21 22int tempXP2; 23int tempYP2; 24 25//ball// 26int xBall=420; 27int yBall=460; 28 29int tempXBall; 30int tempYBall; 31//--------------------------// 32 33int dir=1; //1= up, left. 2= down, left. 3=up, right. 4=down, right.

X-G
Member #856
December 2000
avatar

You still haven't changed your color depth to 32-bit (assuming that's what your desktop is running). Also, are you running this as root? And, finally, make some proper timing logic. rest() is not a good way to do timing.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

HoHo
Member #4,534
April 2004
avatar

I compiled and tested your code and it ran rather nicely on my Linux box in both, 16bit and 32bit when my desktop was 32bit. I think it was around 200-400FPS. It might be because my CPU is quite a bit above the average though.

Also, do you know your rest() doesn't work at all? Problem is you don't install timers. Of cource rest() is not the thing to make your game play at decent framerate. Please learn about timers and proper frame rate keeping. You can start by reading this:
http://www.allegro.cc/forums/thread/589396

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

malleus
Member #8,425
March 2007

Ok I think I added some timers correctly. It runs alot better now. Why does my fps flux? Anyway heres my code. for some unknown reason when I post my code it gets
lots of spaces in it. Just wondering why?

main.cpp

#SelectExpand
1//Pong 2.0 2 3//by Sharif Mabie-Alkhaldi 4 5#include <allegro.h> 6 7#include"variables.h" 8 9 10 11 12 13volatile int ticks=0; 14 15int Frames=0; 16 17int fps=0; 18 19int DidTick=0; 20 21int nticks; 22 23 24#include "misc.h" 25 26 27void ticker() 28 29{ 30 31 ticks++; 32 33}END_OF_FUNCTION(ticker) 34 35 36 37int main() 38 39{ 40 41 allegro_init(); 42 43 install_keyboard(); 44 45 install_timer(); 46 47 set_color_depth(32); 48 49 50 51 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 840, 480, 0, 0); 52 53 54 55 LOCK_VARIABLE(ticks); 56 57 LOCK_FUNCTION(ticker); 58 59 60 61 install_int_ex(ticker, BPS_TO_TIMER(300)); 62 63 64 65 opening=load_bitmap("opening.bmp", 0); 66 67 buffer=create_bitmap(840, 480); 68 69 70 71 start: 72 73 74 75 p1Score=0; 76 77 p2Score=0; 78 79 80 81 blit(opening, buffer, 0, 0, 0, 0, opening->w, opening->h); 82 83 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 84 85 86 87 readkey(); 88 89 clear_to_color( buffer, makecol(0, 0, 0)); 90 91 92 93 94 95 96 97 while(!key[KEY_ESC]) 98 99 { 100 101 tempXP1=xP1; 102 103 tempXP2=xP2; 104 105 106 107 tempYP1=yP1; 108 109 tempYP2=yP2; 110 111 112 113 tempXBall=xBall; 114 115 tempYBall=yBall; 116 117 118 119 nticks=ticks; 120 121 ticks=0; 122 123 124 125 126 127 for(;nticks>0;nticks--) 128 129 { 130 131 132 133 134 135 ballmovement(); 136 137 138 139 player1(); 140 141 player2(); 142 143 144 145 if((xBall+10)==0) 146 147 { 148 149 p2Score++; 150 151 152 153 yBall=460; 154 155 xBall=420; 156 157 } 158 159 else if((xBall-10)==840) 160 161 { 162 163 p1Score++; 164 165 166 167 yBall=460; 168 169 xBall=420; 170 171 } 172 173 174 175 if(p1Score==5) 176 177 { 178 179 textout(screen, font, "Player one wins! Do you want to play again y/esc?", 250, 240, 50); 180 181 readkey(); 182 183 if(key[KEY_Y]) 184 185 goto start; 186 187 } 188 189 else if(p2Score==5) 190 191 { 192 193 textout(screen, font, "Player two wins! Do you want to play again y/esc?", 250, 240, 50); 194 195 readkey(); 196 197 if(key[KEY_Y]) 198 199 goto start; 200 201 } 202 203 DidTick++; 204 205 if(DidTick==60) 206 207 { 208 209 fps=Frames; 210 211 DidTick=0; 212 213 Frames=0; 214 215 } 216 217 } 218 219 clear_to_color(buffer, makecol(0,0,0)); 220 221 draw(); 222 223 224 225 226 227 Frames++; 228 229 }while(!key[KEY_ESC]); 230 231 return 0; 232 233} 234 235 236 237 238 239END_OF_MAIN();

misc.h

#SelectExpand
1void draw(); 2 3void player1(); 4 5void player2(); 6 7void ballMovement(); 8 9 10 11 12 13void draw() 14 15{ 16 17 textprintf_ex(buffer, font, 0, 460, makecol(0, 0, 0), -1, "Player 1 Score: %d", (p1Score-1)); 18 19 textprintf_ex(buffer, font, 0, 460, makecol(255, 255, 255), -1, "Player 1 Score: %d", p1Score); 20 21 22 23 textprintf_ex(buffer, font, 700, 460, makecol(0, 0, 0), -1, "Player 2 Score: %d", (p2Score-1)); 24 25 textprintf_ex(buffer, font, 700, 460, makecol(255, 255, 255), -1, "Player 2 Score: %d", p2Score); 26 27 28 29 rectfill(buffer, tempXP1, tempYP1, (tempXP1+20), (tempYP1+100), makecol(0, 0, 0)); //clear 30 31 rect(buffer, xP1, yP1, (xP1+20), (yP1+100), makecol(255, 255, 255)); //player one paddle 32 33 34 35 rectfill(buffer, tempXP2, tempYP2, (tempXP2+20), (tempYP2+100), makecol(0, 0, 0)); //clear 36 37 rect(buffer, xP2, yP2, (xP2+20), (yP2+100), makecol(255, 255, 255)); //player two paddle 38 39 40 41 circlefill(buffer, tempXBall, tempYBall, 10, makecol(0, 0, 0)); 42 43 circle(buffer, xBall, yBall, 10, makecol(255, 255, 255)); 44 45 46 47 line(buffer, 0, 10, 840, 10, makecol(255, 255, 255)); 48 49 line(buffer, 0, 470, 840, 470, makecol(255, 255, 255)); 50 51 line(buffer, 420, 0, 420, 480, makecol(255, 255, 255)); 52 53 textprintf_ex(buffer, font, 25, 25, makecol(255, 255, 255), -1, "fps: %d", Frames); 54 55 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h); 56 57 58 59} 60 61void player1() 62 63{ 64 65 clear_keybuf(); 66 67 68 69 if(key[KEY_W] && (yP1-2)!=10) 70 71 yP1-=2; 72 73 if(key[KEY_S] && (yP1+102)!=470) 74 75 yP1+=2; 76 77 if(key[KEY_D] && (xP1+22)!=420) 78 79 xP1+=2; 80 81 if(key[KEY_A] && (xP1-2)!=0) 82 83 xP1-=2; 84 85 86 87} 88 89void player2() 90 91{ 92 93 clear_keybuf(); 94 95 96 97 if(key[KEY_UP] && (yP2-2)!=10) 98 99 yP2-=2; 100 101 if(key[KEY_DOWN] && (yP2+102)!=470) 102 103 yP2+=2; 104 105 if(key[KEY_RIGHT] && (xP2+20)!=840) 106 107 xP2+=2; 108 109 if(key[KEY_LEFT] && (xP2-2)!=420) 110 111 xP2-=2; 112 113} 114 115void ballmovement() 116 117{ 118 119 switch(dir) 120 121 { 122 123 case 1: 124 125 if((yBall-10)==10) 126 127 dir=2; 128 129 else if((xBall-10)==(xP1+20)) 130 131 for( int c=yP1; c<(yP1+100);c++) 132 133 if((yBall-10)==c) 134 135 dir=3; 136 137 yBall-=2, xBall-=2; 138 139 break; 140 141 case 2: 142 143 if((yBall+10)==470) 144 145 dir=1; 146 147 else if((xBall-10)==(xP1+20)) 148 149 for( int c=yP1; c<(yP1+100);c++) 150 151 if((yBall-10)==c) 152 153 dir=4; 154 155 yBall+=2, xBall-=2; 156 157 break; 158 159 case 3: 160 161 if((yBall-10)==10) 162 163 dir=4; 164 165 else if((xBall+10)==xP2) 166 167 for( int c=yP2; c<(yP2+100);c++) 168 169 if((yBall+10)==c) 170 171 dir=1; 172 173 yBall-=2, xBall+=2; 174 175 break; 176 177 case 4: 178 179 if((yBall+10)==470) 180 181 dir=3; 182 183 else if((xBall+10)==xP2) 184 185 for( int c=yP2; c<(yP2+100);c++) 186 187 if((yBall+10)==c) 188 189 dir=2; 190 191 yBall+=2, xBall+=2; 192 193 break; 194 195 } 196 197}

</code>

Go to: