MCPcopy Create free account
hub / github.com/GreycLab/CImg / main

Function main

examples/tron.cpp:49–190  ·  view source on GitHub ↗

Main procedure ----------------

Source from the content-addressed store, hash-verified

47// Main procedure
48//----------------
49int main(int argc, char **argv) {
50
51 // Print usage, help and retrieve command line options
52 //-----------------------------------------------------
53 cimg_usage("A very simple Tron game, using the CImg Library");
54 cimg_help("--- Quick help ----------------------------\n"
55 " Player 1 (blue) :\n"
56 " Use keys 'Z' (up), 'S' (down), 'Q' (left)\n"
57 " and 'D' (right) to control your player.\n"
58 " Right 'CONTROL' key enables turbospeed\n"
59 " Player 2 (red) : \n"
60 " Use arrow keys to control your player.\n"
61 " 'TAB' key enables turbospeed.\n"
62 "-------------------------------------------");
63
64 const char *geom = cimg_option("-g","300x300","Size of the game board");
65 const int delay = cimg_option("-s",10,"Game speed (lower value means faster)");
66 const bool twoplayers = !cimg_option("-1",false,"One player only");
67 const int zoom = cimg_option("-z",1,"Zoom factor");
68 const bool full = cimg_option("-f",false,"Fullscreen mode");
69 unsigned int W = 400, H = 400;
70 std::sscanf(geom,"%u%*c%u",&W,&H);
71
72 // Define game colors and variables
73 //----------------------------------
74 const unsigned char blue[] = { 128,200,255}, red[] = { 255,0,0 }, white[] = { 255,255,255 };
75 int score1 = 0, score2 = 0, round_over = 0, ix1 = -1, iy1 = -1, x1 = 0, y1 = 0, u1 = 0, v1 = 0,
76 ix2 = -1, iy2 = -1, x2 = 0, y2 = 0, u2 = 0, v2 = 0;
77 bool start_round = true, turbo1 = false, turbo2 = false;
78
79 // Create background image
80 //--------------------------
81 CImg<unsigned char> background, img;
82 background.assign(64,64,1,3,0).noise(60).draw_plasma().resize(W,H).blur(2).normalize(0,128).
83 draw_rectangle(0,0,W-1,H-1,white,1.0f,~0U);
84
85 // Open display window
86 //---------------------
87 CImgDisplay disp(background,"* CImg-Tron *");
88 if (zoom>1) disp.resize(-100*zoom,-100*zoom);
89 if (full) disp.toggle_fullscreen().display(background);
90
91 // Start main game loop
92 //----------------------
93 while (!disp.is_closed() && !disp.is_keyESC()) {
94
95 // Init new game round if necessary
96 //----------------------------------
97 if (start_round) {
98
99 // Init game variables
100 round_over = 0;
101 ix1 = -1; iy1 = -1; x1 = 10; y1 = 10; u1 = 1; v1 = 0; turbo1 = false;
102 ix2 = -1; iy2 = -1; x2 = W - 11; y2 = H - 11; u2 = -1; v2 = 0; turbo2 = false;
103 img = background;
104 start_round = false;
105
106 // Display a simple pre-round page

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected