Skip to content

Commit ef3178b

Browse files
Advanced keyboard
1 parent 8e4758f commit ef3178b

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "iGraphics.h"
2+
3+
#define min(a, b) a > b ? b : a
4+
#define max(a, b) a < b ? b : a
5+
6+
int ball_1_x = 200, ball_1_y = 200;
7+
int ball_2_x = 300, ball_2_y = 300;
8+
9+
int ball_radius = 20;
10+
int dir_1_x = 0, dir_1_y = 0;
11+
int dir_2_x = 0, dir_2_y = 0;
12+
13+
void iDraw()
14+
{
15+
// Clear the screen
16+
iClear();
17+
18+
// Set drawing color Red
19+
iSetColor(255, 0, 0);
20+
iFilledCircle(ball_1_x, ball_1_y, ball_radius);
21+
22+
// Set drawing color Blue
23+
iSetColor(0, 0, 255);
24+
iFilledCircle(ball_2_x, ball_2_y, ball_radius);
25+
}
26+
27+
void iKeyPress(unsigned char key)
28+
{
29+
switch (key)
30+
{
31+
case 'w': // Move up
32+
dir_1_y = 1;
33+
break;
34+
case 's': // Move down
35+
dir_1_y = -1;
36+
break;
37+
case 'a': // Move left
38+
dir_1_x = -1;
39+
break;
40+
case 'd': // Move right
41+
dir_1_x = 1;
42+
break;
43+
case 'q': // Quit the program
44+
iExitMainLoop();
45+
break;
46+
default:
47+
break;
48+
}
49+
}
50+
51+
void iKeyRelease(unsigned char key)
52+
{
53+
switch (key)
54+
{
55+
case 'w':
56+
case 's':
57+
dir_1_y = 0;
58+
break;
59+
case 'a':
60+
case 'd':
61+
dir_1_x = 0;
62+
break;
63+
default:
64+
break;
65+
}
66+
}
67+
68+
void iSpecialKeyPress(unsigned char key)
69+
{
70+
switch (key)
71+
{
72+
case GLUT_KEY_UP:
73+
dir_2_y = 1;
74+
break;
75+
case GLUT_KEY_DOWN:
76+
dir_2_y = -1;
77+
break;
78+
case GLUT_KEY_LEFT:
79+
dir_2_x = -1;
80+
break;
81+
case GLUT_KEY_RIGHT:
82+
dir_2_x = 1;
83+
break;
84+
default:
85+
break;
86+
}
87+
}
88+
89+
void iSpecialKeyRelease(unsigned char key)
90+
{
91+
switch (key)
92+
{
93+
case GLUT_KEY_UP:
94+
case GLUT_KEY_DOWN:
95+
dir_2_y = 0;
96+
break;
97+
case GLUT_KEY_LEFT:
98+
case GLUT_KEY_RIGHT:
99+
dir_2_x = 0;
100+
break;
101+
default:
102+
break;
103+
}
104+
}
105+
106+
void ballMovement()
107+
{
108+
ball_1_x = max(min(ball_1_x + dir_1_x * 10, iScreenWidth - ball_radius), ball_radius);
109+
ball_1_y = max(min(ball_1_y + dir_1_y * 10, iScreenHeight - ball_radius), ball_radius);
110+
111+
ball_2_x = max(min(ball_2_x + dir_2_x * 10, iScreenWidth - ball_radius), ball_radius);
112+
ball_2_y = max(min(ball_2_y + dir_2_y * 10, iScreenHeight - ball_radius), ball_radius);
113+
}
114+
115+
int main(int argc, char *argv[])
116+
{
117+
iSetTimer(20, ballMovement);
118+
iWindowedMode(400, 400, "iGraphics");
119+
iStartMainLoop();
120+
return 0;
121+
}

iGraphics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ struct CacheEntry
571571
};
572572
static std::unordered_map<std::string, CacheEntry> imageCache;
573573
static std::list<std::string> lruList; // Most recently used at front
574-
static const size_t MAX_CACHE_SIZE = 500;
574+
static const size_t MAX_CACHE_SIZE = 5000;
575575

576576
void iShowImage2(int x, int y, const char *filename, int ignoreColor = -1)
577577
{

0 commit comments

Comments
 (0)