Introduction with Xcode
From C4 Engine Wiki
This tutorial demonstrates how to create and set up a new .dylib target in XCode v3.2. We will create a simple game that shows the text Hello World on the screen. You can use these steps to create your own .dylib to include in C4.
Before starting this tutorial, make sure you can build the C4 source code. See Building the C4 Engine#Building under Mac OS X.
Contents |
Setting up a new target in the C4 project
XCode uses targets to control different build processes. You collect these targets in a project. Looking at the XCode project included in C4, you can see the different targets that are already defined. SimpleBall and SimpleChar are two sample targets that are already perfectly set up to be loaded into C4 as .dylibs. This means all we have to do to create a new .dylib target is copy one of these.
Copy SimpleBall .dylib
Right-click on the SimpleBall target and choose Duplicate.
This creates SimpleBall copy. Right-click on SimpleBall copy and choose Rename, type in Hello, and hit Enter.
We have renamed the target, but we still need to edit one of the build settings that controls the name of the produced .dylib. Right-click on Hello and choose Get Info. You should see the Build info window. Scroll down to Product Name and change it to Hello (Make sure the Configuration drop-down at the top is set to All Configurations). Close the window to save the settings.
Finally, we need to get that old code out of there, so open the Hello target, open Compile Sources, right-click on SimpleBall.cpp and choose Delete. Don't worry, this only deletes from the build settings, not the actual file.
You now have a blank .dylib target that you can add your own code to. Hooray!
Add Files
Now that most of the setup is done, it's time to create our files. I like to keep all of my files in their own separate folder in the Finder, so we will make the folder first, and then add it as a group to our project.
Make a folder called Hello in XCode/C4 (where C4.xcodeproj is located). Now go into the XCode project, right-click on the top C4 under Groups & Files, select Add > Add Existing Files..., and choose your new folder.
Check Hello under Add to targets in the window that pops up (leave Build All checked). The cool thing about doing it this way is that the files you add to this group will also be added to the folder, and any files added to the folder, will be added to your .dylib target.
Code
To create our C++ files, right-click the new Hello group and choose Add > New File.... Select C++, click Next, and name the file Hello.cpp. Notice that XCode automatically creates Hello.h, puts the files in the correct Finder folder, and adds them to the target.
Once our files are created, we want to add the following code. For the fun of it will add our own namespace so we don't pollute C4.
Hello.h:
#ifndef Hello_h #define Hello_h #include "C4Application.h" #include "C4Interface.h" extern "C" { module_export C4::Application* ConstructApplication(); } using namespace C4; namespace HW { class Game : public Singleton<Game>, public Application { public: Game(); ~Game(); }; extern Game* game; } #endif // Hello_h
Hello.cpp:
#include "Hello.h" using namespace C4; using namespace HW; Game* HW::game = 0; C4::Application* ConstructApplication() { return (new Game); } Game::Game() : Singleton<Game>(game) { // Add some text on screen Font* font = Font::Get("Bold"); TextElement* text = new TextElement("Hello World!", font); text->SetElementPosition(Point3D(50.0f, 50.0f, 0.0f)); TheInterfaceMgr->AddElement(text); // Report a message to the log file Engine::Report("<br /><div>Hello World!</div>", kReportLog); } Game::~Game() { }
Configure Variables
Now we can build our project, so build it and all should be well. You should see Hello.dylib under the Products group. But we're not quite done yet. We need to change the C4 configuration files so that C4 knows to load our application when it starts up. To do this navigate to your base C4 directory. Once there navigate to the "Data" directory, and then to the "Engine" directory. Open up the file "Variables.cfg" and change "Game" to the name of your application ("Hello" in our case) for the following variables.
$applicName = "Hello"; $gameName = "Hello";
Run The Game
Ok, now we're ready to run our Hello game. Navigate to the location of the C4.app (Xcode/C4/build/ and then either Debug or Release depending on what configuration you built), and run it.
Checking The Log
One thing left, we want to see the log message that we wrote in our code. To see this Ctrl Click on the C4.app and select "Show Package Contents". Navigate to "Contents/Resources" and open the file C4Log.html. Now scroll to the bottom and you should see our log message "Hello World!".
Setting up a new project
TODO




