1  | #include "stdafx.h"
  | 
2  | #include <string.h>
  | 
3  | 
  | 
4  | /* -- Include the precompiled libraries -- */
  | 
5  | #ifdef WIN32
  | 
6  | #pragma comment(lib, "SDL.lib")
  | 
7  | #pragma comment(lib, "SDLmain.lib")
  | 
8  | #pragma comment(lib, "SDL_image.lib")
  | 
9  | #endif
  | 
10  | 
  | 
11  | #include "SDL.h"
  | 
12  | #include "SDL_image.h"
  | 
13  | #include <SDL_gfxPrimitives.h>
  | 
14  | 
  | 
15  | 
  | 
16  | #include "bodies.h"
  | 
17  | 
  | 
18  | SDL_Surface *screen, *image[6];
  | 
19  | SDL_Event e;
  | 
20  | SDL_Rect rect,background;
  | 
21  | 
  | 
22  | unsigned char *bKey;
  | 
23  | int quit = 0;
  | 
24  | unsigned int i=0;
  | 
25  | 
  | 
26  | #define BOARD_LINE_WIDTH 6      // Width of each of the two lines that delimit the board
  | 
27  | #define BLOCK_SIZE 16        // Width and Height of each block of a piece
  | 
28  | #define BOARD_POSITION 320      // Center position of the board from the left of the screen
  | 
29  | #define BOARD_WIDTH 10        // Board width in blocks 
  | 
30  | #define BOARD_HEIGHT 20        // Board height in blocks
  | 
31  | #define MIN_VERTICAL_MARGIN 20    // Minimum vertical margin for the board limit     
  | 
32  | #define MIN_HORIZONTAL_MARGIN 20  // Minimum horizontal margin for the board limit
  | 
33  | #define PIECE_BLOCKS 5  
  | 
34  | 
  | 
35  | int mBoard [BOARD_WIDTH][BOARD_HEIGHT];
  | 
36  | enum { POS_FREE, POS_FILLED };
 | 
37  | enum color {BLACK, RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, COLOR_MAX};
 | 
38  | static Uint32 mColors [COLOR_MAX] = {0x000000ff,0xff0000ff, 0x00ff00ff, 0x0000ffff,
 | 
39  |                                0x00ffffff, 0xff00ffff, 0xffff00ff,0xffffffff};
  | 
40  | 
  | 
41  | void InitBoard(void);
  | 
42  | void StorePiece (int pX, int pY, int pPiece, int pRotation);
  | 
43  | void DrawPiece (int pX, int pY, int pPiece, int pRotation);
  | 
44  | 
  | 
45  | void InitBoard(void)
  | 
46  | {
 | 
47  |   for (int i = 0; i < BOARD_WIDTH; i++)
  | 
48  |     for (int j = 0; j < BOARD_HEIGHT; j++)
  | 
49  |       mBoard[i][j] = POS_FREE;
  | 
50  | }
  | 
51  | 
  | 
52  | void StorePiece (int pX, int pY, int pPiece, int pRotation)
  | 
53  | {
 | 
54  |   // Store each block of the piece into the board
  | 
55  |   for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
  | 
56  |   {
 | 
57  |     for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
  | 
58  |     {  
 | 
59  |       // Store only the blocks of the piece that are not holes
  | 
60  |       if (GetBlockType (pPiece, pRotation, j2, i2) != 0)    
  | 
61  |         mBoard[i1][j1] = POS_FILLED;  
  | 
62  |     }
  | 
63  |   }
  | 
64  | }
  | 
65  | 
  | 
66  | void DrawRectangle (int pX1, int pY1, int pX2, int pY2, enum color pC)
  | 
67  | {
 | 
68  |   boxColor(screen, pX1, pY1, pX2, pY2-1, mColors[pC]);
  | 
69  | }
  | 
70  | 
  | 
71  | int GetXPosInPixels (int pPos)
  | 
72  | {
 | 
73  |   return  ( ( BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) );
  | 
74  | }
  | 
75  | 
  | 
76  | int GetYPosInPixels (int pPos)
  | 
77  | {
 | 
78  |   return ( (screen->h - (BLOCK_SIZE * BOARD_HEIGHT)) + (pPos * BLOCK_SIZE) );
  | 
79  | }
  | 
80  | 
  | 
81  | 
  | 
82  | void DrawPiece (int pX, int pY, int pPiece, int pRotation)
  | 
83  | {
 | 
84  |   color mColor;        // Color of the block 
  | 
85  | 
  | 
86  |   // Obtain the position in pixel in the screen of the block we want to draw
  | 
87  |   int mPixelsX = GetXPosInPixels (pX);
  | 
88  |   int mPixelsY = GetYPosInPixels (pY);
  | 
89  | 
  | 
90  |   // Travel the matrix of blocks of the piece and draw the blocks that are filled
  | 
91  |   for (int i = 0; i < PIECE_BLOCKS; i++)
  | 
92  |   {
 | 
93  |     for (int j = 0; j < PIECE_BLOCKS; j++)
  | 
94  |     {
 | 
95  |       
  | 
96  |       if (GetBlockType(pPiece, pRotation, j, i) != 0)
  | 
97  |         DrawRectangle(mPixelsX + i * BLOCK_SIZE, 
  | 
98  |                 mPixelsY + j * BLOCK_SIZE, 
  | 
99  |                (mPixelsX + i * BLOCK_SIZE) + BLOCK_SIZE - 1, 
  | 
100  |                (mPixelsY + j * BLOCK_SIZE) + BLOCK_SIZE - 1, 
  | 
101  |                 mColor);
  | 
102  |     }
  | 
103  |   }
  | 
104  | }
  | 
105  | 
  | 
106  | int main(int argc, char **argv)
  | 
107  | {
 | 
108  | ....
  | 
109  | }
  |