Posted By

ozkriff on 01/08/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

eval


TETRIS - SDLOpenGLplain C


 / Published in: C
 

  1. #include "SDL/SDL.h"
  2. #include "SDL/SDL_opengl.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #define TICK_INTERVAL 30
  8.  
  9. #define TRUE 1
  10. #define true TRUE
  11. #define FALSE 0
  12. #define false FALSE
  13.  
  14. int SCREEN_WIDTH = 300;
  15. int SCREEN_HEIGHT = 600;
  16. int SCREEN_BPP = 32;
  17.  
  18. /*#undef main*/ /* allows write straight to console */
  19.  
  20. SDL_Surface *surface;
  21. int VIDEO_FLAGS; /* Flags to pass to SDL_SetVideoMode */
  22.  
  23. int DONE = 0; /* Main loop variable. (val 1 = QUIT*/
  24.  
  25. #define PIECE_SIZE 5
  26. int PIECE[PIECE_SIZE][PIECE_SIZE] = {0};
  27.  
  28. #define MAP_HEIGHT 20
  29. #define MAP_WIDTH 10
  30. int MAP[MAP_HEIGHT][MAP_WIDTH] = {0};
  31.  
  32. int IS_ROTATEBLE = 0; /* can piece be rotated? */
  33.  
  34. int PIECE_X = 0, PIECE_Y = 0; /* position on MAP */
  35.  
  36.  
  37.  
  38.  
  39.  
  40. void
  41. quit (int return_code)
  42. {
  43. SDL_Quit(); /* clean up the window */
  44. exit(return_code); /* and exit appropriately */
  45. }
  46.  
  47.  
  48.  
  49.  
  50. void
  51. resize_window (int width, int height)
  52. {
  53. SCREEN_WIDTH = width;
  54. SCREEN_HEIGHT = height;
  55.  
  56. glViewport(0, 0, width, height);
  57.  
  58. glMatrixMode(GL_PROJECTION);
  59. glLoadIdentity();
  60. glOrtho(0, width, 0, height, - 1, 1);
  61.  
  62. /* Make sure we're chaning the model view and not the projection */
  63. glMatrixMode(GL_MODELVIEW); /* just to make sure */
  64. glLoadIdentity();
  65.  
  66.  
  67. /* glPointSize((width + height) / 50); */
  68. }
  69.  
  70.  
  71.  
  72.  
  73. void
  74. clear_piece()
  75. {
  76. int i, j;
  77.  
  78. for(i = 0; i < PIECE_SIZE; i++){
  79. for(j = 0; j < PIECE_SIZE; j++){
  80. PIECE[i][j] = 0;
  81. }
  82. }
  83. }
  84.  
  85.  
  86.  
  87.  
  88. void
  89. clear_map()
  90. {
  91. int i, j;
  92.  
  93. for(i = 0; i < MAP_HEIGHT; i++){
  94. for(j = 0; j < MAP_WIDTH; j++){
  95. MAP[i][j] = 0;
  96. }
  97. }
  98. }
  99.  
  100.  
  101.  
  102.  
  103. void
  104. rotate_piece()
  105. {
  106. int i, j;
  107. int old_piece[PIECE_SIZE][PIECE_SIZE];
  108.  
  109. if(IS_ROTATEBLE){
  110. /*copy piece*/
  111. for(i = 0; i < PIECE_SIZE; i++){
  112. for(j = 0; j < PIECE_SIZE; j++){
  113. old_piece[i][j] = PIECE[i][j];
  114. }
  115. }
  116.  
  117. /*rotate*/
  118. for(i = 0; i < PIECE_SIZE; i++){
  119. for(j = 0; j < PIECE_SIZE; j++){
  120. PIECE[4-i][j] = old_piece[j][i];
  121. }
  122. }
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129. void
  130. store_piece()
  131. {
  132. int px, py; /*piece*/
  133. int mX, mY; /*map*/
  134.  
  135. for(py = 0; py < PIECE_SIZE; py++){
  136. for(px = 0; px < PIECE_SIZE; px++){
  137. if(PIECE[py][px] != 0){
  138. mX = PIECE_X + px;
  139. mY = PIECE_Y + py;
  140. if((mX >= 0) && (mX < MAP_WIDTH) && (mY >= 0) && (mY < MAP_HEIGHT)){
  141. MAP[mY][mX] = PIECE[py][px];
  142. }
  143. }
  144. }
  145. }
  146. }
  147.  
  148.  
  149.  
  150.  
  151. int
  152. is_possible_movement()
  153. {
  154. int px, py; /*piece*/
  155. int mX, mY; /*map*/
  156.  
  157. for(mX = PIECE_X, px = 0; mX < (PIECE_X + PIECE_SIZE); mX++, px++){
  158. for(mY = PIECE_Y, py = 0; mY < (PIECE_Y + PIECE_SIZE); mY++, py++){
  159. if(PIECE[py][px] != 0){
  160. /*The piece is outside the limits of the board*/
  161. if((mX < 0) || (mX >= MAP_WIDTH) || (mY >= MAP_HEIGHT)){
  162. return 0;
  163. }
  164.  
  165. /*The piece have collisioned with a block already stored in the map*/
  166. if((mY >= 0) && (MAP[mY][mX] != 0)){
  167. return 0;
  168. }
  169. }
  170. }
  171. }
  172.  
  173. return 1;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. void
  180. delete_line(int mY)
  181. {
  182. int x, y;
  183.  
  184. // Moves all the upper lines one row down
  185. for(y = mY; y > 0; y--){
  186. for(x = 0; x < MAP_WIDTH; x++){
  187. MAP[x][y] = MAP[x][y-1];
  188. }
  189. }
  190. }
  191.  
  192.  
  193.  
  194.  
  195. void
  196. delete_possible_lines()
  197. {
  198. int x, y;
  199.  
  200. for(y = 0; y < MAP_HEIGHT; y++){
  201. x = 0;
  202. while(x < MAP_WIDTH){
  203. if(MAP[x][y] == 0)
  204. break;
  205. x++;
  206. }
  207.  
  208. if (x == MAP_WIDTH)
  209. delete_line(y);
  210. }
  211. }
  212.  
  213.  
  214.  
  215.  
  216. void
  217. load_O()
  218. {
  219. clear_piece();
  220. PIECE[2][2] = 3;
  221. PIECE[3][2] = 2;
  222. PIECE[3][3] = 2;
  223. PIECE[2][3] = 2;
  224. IS_ROTATEBLE = 0;
  225. PIECE_X = 2;
  226. PIECE_Y = -2;
  227. }
  228.  
  229.  
  230.  
  231.  
  232. void
  233. load_L()
  234. {
  235. clear_piece();
  236. PIECE[1][2] = 2;
  237. PIECE[2][2] = 3;
  238. PIECE[3][2] = 2;
  239. PIECE[3][3] = 2;
  240. IS_ROTATEBLE = 1;
  241. PIECE_X = 2;
  242. PIECE_Y = -2;
  243. }
  244.  
  245.  
  246.  
  247.  
  248. int
  249. is_game_over()
  250. {
  251. int i;
  252.  
  253. for(i = 0; i < MAP_WIDTH; i++){
  254. if(MAP[0][i] != 0){
  255. return 1;
  256. }
  257. }
  258. return 0;
  259. }
  260.  
  261.  
  262.  
  263. void
  264. draw_map()
  265. {
  266. int y, x;
  267.  
  268. glBegin(GL_POINTS);
  269. for(y = 0; y < MAP_HEIGHT; y++){
  270. for(x = 0; x < MAP_WIDTH; x++){
  271. if(MAP[y][x] != 0){
  272. glColor3f(1, 1, 1);
  273. }else{
  274. glColor3f(0.1, 0.1, 0.1);
  275. }
  276. glVertex2d(SCREEN_WIDTH/MAP_WIDTH * x + 20,
  277. SCREEN_HEIGHT/MAP_HEIGHT * y + 20);
  278. }
  279. }
  280. glEnd();
  281. }
  282.  
  283.  
  284.  
  285.  
  286. void
  287. draw_piece(int pX, int pY)
  288. {
  289. int I, J;
  290. float x, y;
  291.  
  292. glColor3f(1, 0, 0);
  293. glBegin(GL_POINTS);
  294. for(I = 0; I < PIECE_SIZE; I++){
  295. for(J = 0; J < PIECE_SIZE; J++){
  296. if(PIECE[I][J] != 0){
  297. x = SCREEN_WIDTH/MAP_WIDTH * (J + pX);
  298. y = SCREEN_HEIGHT/MAP_HEIGHT * (I + pY);
  299. glVertex2d(x + 20, y + 20);
  300. }
  301. }
  302. }
  303. glEnd();
  304. }
  305.  
  306.  
  307.  
  308. void
  309. draw()
  310. {
  311. glClear(GL_COLOR_BUFFER_BIT);
  312.  
  313. draw_map();
  314. draw_piece(PIECE_X, PIECE_Y);
  315.  
  316. SDL_GL_SwapBuffers();
  317. }
  318.  
  319.  
  320.  
  321. void
  322. hanlde_events (SDL_Event event)
  323. {
  324. switch(event.type){
  325. default:
  326. /* puts ("unknown event!"); */
  327. break;
  328. case SDL_VIDEORESIZE:
  329. surface = SDL_SetVideoMode(event.resize.w,
  330. event.resize.h,
  331. 32, SDL_OPENGL);
  332. if(!surface){
  333. fprintf(stderr, "no surface after resize: %s\n",
  334. SDL_GetError());
  335. quit(1);
  336. }
  337. resize_window(event.resize.w, event.resize.h);
  338. break;
  339. case SDL_KEYDOWN:
  340. switch(event.key.keysym.sym){
  341. default:
  342. break;
  343. case SDLK_q:
  344. case SDLK_ESCAPE:
  345. DONE = 1;
  346. quit(0);
  347. break;
  348. case SDLK_F1:
  349. SDL_WM_ToggleFullScreen(surface);
  350. break;
  351. case SDLK_s:
  352. case SDLK_DOWN:
  353. while(is_possible_movement()){
  354. PIECE_Y++;
  355. }
  356. PIECE_Y--;
  357. store_piece();
  358. delete_possible_lines();
  359. load_L();
  360. if(!is_possible_movement()){
  361. DONE = 1;
  362. quit(0);
  363. }
  364. break;
  365. case SDLK_w:
  366. case SDLK_UP:
  367. rotate_piece();
  368. if(!is_possible_movement()){
  369. rotate_piece();
  370. rotate_piece();
  371. rotate_piece();
  372. }
  373. break;
  374. case SDLK_a:
  375. case SDLK_LEFT:
  376. PIECE_X--;
  377. if(!is_possible_movement()){
  378. PIECE_X++;
  379. }
  380. break;
  381. case SDLK_d:
  382. case SDLK_RIGHT:
  383. PIECE_X++;
  384. if(!is_possible_movement()){
  385. PIECE_X--;
  386. }
  387. break;
  388. }
  389. break;
  390. case SDL_QUIT:
  391. DONE = 1;
  392. break;
  393. }
  394. }
  395.  
  396.  
  397.  
  398.  
  399. void
  400. init ()
  401. {
  402. SDL_Init(SDL_INIT_EVERYTHING);
  403. SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_OPENGL);
  404.  
  405. glClearColor(0, 0, 0, 1);
  406. glClearDepth(1);
  407.  
  408. glPointSize(8);
  409. /*glLineWidth(4);*/
  410.  
  411. resize_window(SCREEN_WIDTH, SCREEN_HEIGHT);
  412. }
  413.  
  414.  
  415.  
  416.  
  417. int
  418. main(int argc, char **argv)
  419. {
  420. SDL_Event event; /* used to collect events */
  421. Uint32 next_time;
  422. Uint32 vertical_move;
  423.  
  424. init();
  425. load_L();
  426.  
  427. next_time = SDL_GetTicks() + TICK_INTERVAL;
  428. vertical_move = SDL_GetTicks();
  429.  
  430. while(!DONE){
  431. while(SDL_PollEvent(&event))
  432. hanlde_events(event);
  433.  
  434. draw();
  435.  
  436. if(vertical_move + 750 < SDL_GetTicks()){
  437. PIECE_Y++;
  438. if(!is_possible_movement()){
  439. PIECE_Y--;
  440. store_piece();
  441. delete_possible_lines();
  442.  
  443. load_L();
  444. if(!is_possible_movement()){
  445. DONE = 1;
  446. quit(0);
  447. }
  448. }
  449. vertical_move = SDL_GetTicks();
  450. }
  451.  
  452. SDL_Delay(next_time - SDL_GetTicks());
  453. next_time += TICK_INTERVAL;
  454. }
  455.  
  456. quit(0);
  457.  
  458. return 0;
  459. }

Report this snippet  

You need to login to post a comment.