Debian Codegraph @

Documentation/Tutorial/TicTacToe/part-1.html

raw
1 <HTML>
2 <HEAD>
3 <TITLE>ClanLib Game SDK: ClanLib - Documentation</TITLE>
4 <STYLE TYPE="text/css"><!--
5 HTML BODY
6 {
7 font-family: verdana, helvetica, sans-serif;
8 font-size: 14px;
9 border-style: solid;
10 }
11 H1 { font-size: 22px; }
12 H2 { font-size: 18px; }
13 H3 { font-size: 16px; }
14 H4 { font-size: 14px; }
15 P { font-size: 14px; }
16 LI { font-size: 14px; }
17 --></STYLE>
18 </HEAD>
19
20 <body bgcolor=white text=black>
21 <center>
22 <table width=70%>
23 <tr>
24 <td>
25
26 <center><table><tr><td>
27 <img
28 SRC="../../Images/clanlib_logo_small.png"
29 alt="ClanLib logo"><br>
30 </td></tr></table></center>
31
32 <h1>Simple Game Development with ClanLib 0.8</h1>
33 <h2>Part 1</h2>
34 <b>Introduction to ClanLib & Visual C++ Workspace Setup</b>
35
36 <ul>
37 <p>
38 <i>This tutorial assumes you have a working knowledge of C++ or a good reference from which to learn. We will be designing classes, using pointers, and doing other moderately-advanced things.</i>
39 <br><br>
40 That out of the way, let's start with this question: What is ClanLib? It is a programming library that handles mundane things like drawing graphics, playing sounds, and accepting user input from the mouse and keyboard. It also does much more, but even these simple things can be very difficult if you're creating them from scratch. ClanLib has the added bonus of being a cross-platform library, which means that you can use the same source code you use to build a Windows game to also build that game under Linux (or vice versa.)<br>
41 <br>
42 The first step in creating a game with ClanLib (CL) is downloading, installing, and (optionally) compiling the library. We will be using the static libraries that you can download or compile yourself. There are other ways of using the library, such as compiling dynamic DLLs, but we'll ignore those. If you have trouble with this first step there is good documentation to help you out, so I'll be moving along to the next step: setting up the Visual C++ workspace.<br>
43 <br>
44 First, create a new, empty workspace with the name of your game. The sample game we'll be developing here is Tic-Tac-Toe (more on that a bit later).<br>
45 </p>
46
47 <b>Fig 1</b>.
48 <div align="center"><img src="figure1.png" width="561" height="400" alt="" border="0"></div><br><br>
49
50 <p>
51 Next, go into <i>Project > Settings > Link</i> and add the CL libraries you'll be using to the Object/library modules field. For the Debug project configuration, I've added the basic debug libraries we'll need:<b> clanCored.lib clanAppd.lib clanDisplayd.lib </b> For the Release configuration, we'll use the same libraries without the 'd' appended to the name. (Note: this is apparently now optional - if you include the CL headers, these libraries are automatically linked.) Also for the Debug configuration, I've added the entry <b>/nodefaultlib:"libcmt"</b> to the options field. This tells the compiler to ignore the that particular library because it would duplicate symbols (variables, functions) defined in another library.
52 </p>
53
54 <b>Fig 2</b>.
55 <div align="center"><img src="figure2.png" width="577" height="372" alt="" border="0"></div>
56
57 <p>
58 Last in setting up, go into <i>Project > Settings > C++</i> and change the category to 'code generation'. Under 'run-time library', select <b>Debug Multithreaded</b>. (For your release configuration, choose plain <b>Multithreaded</b>.)
59 </p>
60
61 <b>Fig 3</b>.
62 <div align="center"><img src="figure3.png" width="577" height="372" alt="" border="0"></div>
63
64 <p>
65 At this point I like to test the new application's setup to make sure everything compiles and all the header files are located. You may have to add the CL library and include directories to VC++'s configuration under <i>Tools > Options > Directories.</i><br>
66 <br>
67 To test the setup, just grab the file 'minimum.cpp' from the Minimum example program included with the CL release. Add it to your source files folder and try compiling. If it compiles and runs correctly, you can go ahead and delete it to have a clean slate for your game.
68 </ul>
69
70 <b>CL_ClanApplication</b>
71 <ul>
72 Every CL application must derive this class and create an instance of the derived class. This instances forms the base of your program, and you add whatever code you need to the main() function. Here is a simple derived class that will form the basis of our program.
73 <br><br>
74 <i>A quick note about style - I got my programming start with Java, so my C++ style reflects this. It differs somewhat from the CL API but I don't either style has a particular advantage. Just don't get me started on Hungarian notation..</i><br><br>
75
76 </p>
77
78 <div align="center"><table border=1 bordercolor="#000000" cellpadding=5 cellspacing=0><tr><td>
79 <pre>// Interface (for the .h file)
80 // Include necessary CL header files
81 #include &lt;ClanLib/core.h&gt;
82 #include &lt;ClanLib/display.h&gt;
83 #include &lt;ClanLib/gl.h&gt;
84 #include &lt;ClanLib/application.h&gt;
85
86 class TicTacToeApp : public CL_ClanApplication {
87
88 public:
89 TicTacToeApp();
90 ~TicTacToeApp();
91
92 virtual int main(int, char **);
93 };</pre>
94 </td></tr></table></div>
95
96 <br><br>
97
98 <div align="center"><table border=1 bordercolor="#000000" cellpadding=5 cellspacing=0><tr><td>
99 <pre>// Implementation (for the .cpp file)
100 #include "TicTacToeApp.h"
101
102 // This application instance is required or the app will not run
103 TicTacToeApp applicationInstance;
104
105 // Constructor
106 TicTacToeApp::TicTacToeApp() {}
107
108 // Destructor
109 TicTacToeApp::~TicTacToeApp() {}
110
111 int TicTacToeApp::main(int, char **) {
112
113 // Create a console window for text-output if not available
114 CL_ConsoleWindow console("TicTacToeApp Console");
115 console.redirect_stdio();
116
117 try {
118 // CL initialization functions
119 // These must be called or CL functions will not work
120 // Also, SetupCore must be init()'ed first and denit()'ed last
121 CL_SetupCore::init();
122 CL_SetupDisplay::init();
123 CL_SetupGL::init();
124
125 // Set display mode
126 CL_DisplayWindow window("TicTacToeApp", 500, 450);
127
128 // CL deinitialization functions
129 CL_SetupGL::deinit();
130 CL_SetupDisplay::deinit();
131 CL_SetupCore::deinit();
132 } catch (CL_Error err) {
133 std::cout << "Exception caught: " << err.message.c_str() << std::endl;
134
135 // Display console close message and wait for a key
136 console.display_close_message();
137 }
138
139 return 0;
140 }</pre>
141 </td></tr></table></div>
142 <br>
143
144 <p>
145 Of course, this program does nothing but open and close, but if it does that then we're off to a good start. Now for a momentary time-out that every programmer should take before diving into the creation of their next masterpiece.</p>
146 </ul>
147
148 <b>Tic-Tac-Toe</b>
149
150 <ul>
151 <p>
152 Games don't come any simpler than this. I plan on creating 2 classes for this application - the TicTacToeApp we've already started, and a TicTacToeGame for actually playing with X's and O's. Note that in this tutorial I will be focusing on using CL to create this game and not the game itself, so I will skip over large portions of game implementation. That's experience you will have to pick up somewhere else.<br>
153 <br>
154 We're ready to start adding graphics and control code to our game, so that's it for this part of the tutorial.<br>
155 <br>
156 <a href="clanlib-tutorial-part1.zip">Download Visual C++ Workspace for Part 1</a><br>
157 <br>
158 <a href="part-2.html">Move on to Part 2</a>
159
160 </p>
161
162 </ul>
163
164 </TD>
165 </TR>
166 </TABLE>
167
168 </CENTER>
169 </BODY>
170 </HTML>
171