C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

...του ubuntu και έργων ΕΛ/ΛΑΚ (Έργα-Οδηγοί-Προτάσεις)

Συντονιστής: konnn

C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό migf1 » 03 Ιαν 2012, 04:52

EDIT-4
Το παιχνίδι έχει πια δικιά του ιστοσελίδα: viewtopic.php?p=254631#p254631

EDIT-3:
Παρατηρώ ότι ο κόσμος κατεβάζει την αρχική έκδοση 1.00 αντί για την 1.03a. Το link της 1.03a είναι εδώ: https://www.box.com/files/0/f/220124730 ... 1552308520 και οδηγίες για τη διόρθωση κάποιων παραλείψεων του Readme βρσίκονται εδώ: viewtopic.php?p=229197#p229197 και στο EDIT-1, στην παράθεση που ακολουθεί στην αμέσως επόμενη παράγραφο από αυτήν που διαβάζετε τώρα.

EDIT-2:
Δοκιμαστική έκδοση 1.03a με Ελληνικά: viewtopic.php?p=229197#p229197

EDIT-1: Υπάρχει παράλειψη στο Readme αρχείο. Για επιτυχές compilation σε Unix/Linux η γραμμή εντολών πρέπει να είναι η εξής:
gcc -Wall gtk_tic.c -o gtk_tic -export-dynamic `pkg-config --cflags --libs gtk+-2.0`

Ευχαριστώ το φίλο medigreek που το ανακάλυψε!


Παραθέτω μια τρίλιζα που έγραψα σε C για να ξεσκονίσω λίγο το GTK. Την ανέπτυξα σε Windows αλλά ο κώδικας είναι cross-platform. Επειδή αυτό τον καιρό είμαι χωρίς Linux θα ήθελα αν μπορεί κάποιος να μου επιβεβαιώσει ότι μπορεί να το κάνει compile κανονικά και πως τρέχει οκ. Έχω 2 glade files, ένα Αγγλικό κι ένα Ελληνικό που προς το παρόν είναι απενεργοποιημένο (αν και μπορείτε να το μετονομάσετε με το όνομα του Αγγλικού, ή ακόμα καλύτερα μέσα στον κώδικα να αλλάξετε το...

Κώδικας: Επιλογή όλων
#define GLADE_FNAME   "gui/gtk_tic_en.glade"

σε
Κώδικας: Επιλογή όλων
#define GLADE_FNAME   "gui/gtk_tic_el.glade"

και να το ξανα-κάνετε compile. Σημειώστε όμως πως ορισμένα text-labels ανανεώνονται δυναμικά μέσα από τον κώδικα, οπότε αυτά θα εξακολουθούν να εμφανίζονται στα Αγγλικά. Τις επόμενες μέρες θα προσθέσω κανονική υποστήριξη Ελληνικών.

Μερικά screenshots...





Και το zip...

http://www.box.com/s/p711t4ln9j8rxopacs85

Περιέχει...

  • τον κώδικα (2256 γραμμές, μαζί με τα σχόλια, σε ANSI C89 + GTK+2)
  • δυο Win32 εκτελέσιμα (ένα με console window, compiled με MingW32 GCC 4.6.2, κι ένα χωρίς console window, compiled με Pelles C 6.00)
  • τον φάκελο \gui με τις bitmap εικόνες και 2 glade files (ένα Αγγλικό κι ένα πειραματικό Ελληνικό)
  • Readme.txt (σημειώσεις για τον κώδικα και λεπτομερείς οδηγίες για compilation σε διάφορες πλατφόρμες, με έμφαση στα Windows, μιας και στο Unix/Linux το GTK+ είναι συνήθως προ-εγκατεστημένο)

Το Readme είναι αρκετά μεγάλο, είναι στα Αγγλικά και περιέχει ενδιαφέρουσες πληροφορίες για όποιον θελήσει να ασχοληθεί με τον κώδικα (περιγράφει τη γενικότερη δομή και τα abstractions για την Object Oriented προσέγγιση). Περιέχει επίσης μεταξύ άλλων οδηγίες για εγκατάσταση του GTK+ και του Glade+3 σε Windows, καθώς επίσης και του Code::Blocks (όλα free και cross-platform) για όποιον θελήσει να μεταφέρει λίγο Linux feeling στα Windows του.

Το βάζω κι εδώ σε spoiler, σε περίπτωση που προτιμάτε να το διαβάσετε από εδώ (αν και του χαλάει τη στοίχιση):

Spoiler: show
/* ========================================================================
* PROJECT : TIC-TAC-TOE Overkill
* AUTHOR : migf1 <mig_f1@hotmail.com>
* FILE : gtk_tic.c
* LANGUAGE : C (C89 + GTK+2)
* PLATFORM : Cross-platform
*
* DESCRIPTION : A classic tic-tac-toe game featuring:
* - cross-platform GUI ( GTK+2 )
* - 4 modes ( Human vs CPU, CPU vs Hum, CPU vs CPU, Hum vs Hum )
* - 3 levels of difficulty ( Expert, Casual, Novice )
*
* LICENSE : No License! Consider it freeware, public domain or whatever, and do
* whatever you want with it. All I ask is to reference my name and
* include the original package of the game if you decide to redistribute
* it. Oh, I'll be happy to see it ported or improved, so drop me a note
* if you feel like it :)
* ========================================================================
*/


==============================================================================
I M P L E M E N T A T I O N N O T E S
==============================================================================

The notes in this section will hopefully help you to get around the source code of this little game, although most of the source code contains commentary.

First I need to... confess that I was too lazy to properly design the structure of the program in a piece of paper before coding it, so things are not that well organized as they should (but they are not too bad either).


Environments
------------

The general idea was to separate the GUI from the game itself, so the action takes place mostly in two environments: the gaming environment, represented by the custom type: Game, and the GUI environment, represented by the custom type: Gui. Those two environments are abstract siblings inside the general environment of the program, represented by the custom type: GenEnv.

The general environment is responsible for controlling the interaction between the game and the GUI environments, while performing higher level tasks. As you can see in the source code, things didn't go exactly as intended, because as I explained above I was too lazy to thoughtfully prepare the design of the program before getting to code it. As a result, I've ended up with a few stray functions, not clearly belonging to any distinct environment. I left them "floating" around, tagging their names with the prefix "util_" (meaning: utility functions).

Speaking of prefixes, gui related functions have their names prefixed with "gui_", while game related functions have their names prefixed with "game_". Functions related to the general environment, have their names prefixed with "env_".

The gaming environment ( Game ) has a child environment, represented by the custom type: GamePlay, with related functions having the "gameplay_" prefix in their names. This GamePlay environment is not what its name may suggest at a first glance: it does not refer to the general game play. Instead, it refers to a single round inside a game (a single play). So a perhaps more intuitive name for this custom type would be something like: Round, but I started the code using Play as the name, later changed it to GamePlay, and I figured that once explained it is not that misleading after all.

There is more data-structure nesting going on in the source code (for example the custom types PlayBoard and Player, belonging to the GamePlay environment, or the custom types GamePrefs and GamePlaystats belonging to the Game environment) but those can be hardly thought as complete environments by themselves. So just think of them simply as interior parts of the basic environment they belong to.


Object Oriented Approach
------------------------

The above design is an Object Oriented approach, and actually most of those functions do operate on pointers to abstract objects, which are passed at the start of the argument list of the functions. So "env_" functions have their 1st argument being of type GenEnv *, while "game_" functions have their 1st argument being of type: Game *, and "gameplay_" functions have their 1st argument being of type: GamePlay *, and so on.

Exceptions to this OO convention are the "util_" and "debug_" functions, for the reasons I explained earlier (stray functions).


AI Algorithm
------------

Now a few words about the algorithms used for the 3 levels of AI strength. As you may already know, the most popular (and recommended) way to implement the Tic-Tac-Toe AI is using a game-tree, along with minmax heuristics, with or without alpha-pruning (better with).

I don't remember if I have mentioned it so far, but sometimes I can be very lazy! That's why I didn't took that route in the first place, for the Expert level. It turned out that if I was, I would have finished the game a lot earlier, and most importantly with a much cleaner AI code.

Nevertheless I did it with a variation of the brutal algorithm, using (as it again turned out to be) a not-that-good heuristic function for evaluating the board. Furthermore, I didn't implement backtracking when evaluating the board, thus I was forced to add a really ugly, brutal and frankly a quite messy function for blocking opponent forks. That function is called: util_ai_rank_fork_adjustment()... I didn't even manage to properly embed it into any of my environments :p (the latter holds for the board evaluation function too: util_ai_eval_board() ).

Anyway, at least it works :)

When in Casual strength level (Medium difficulty) the AI does not deal with opponent forks at all. It only checks for squares that lead to either an own or an opponent win. When in Novice strength level (Easy) the AI plays completely randomly!


GUI Implementation
------------------

For implementing the GUI I chose GTK+2, the de-facto cross-platform API for implementing graphical user interfaces with C. Since I have coupled it with standard C89 code, you can compile the source code on a vast variety of platforms, without changing anything.

On the other hand, it requires some extra effort for installing GTK+2 and all its dependencies on platforms that do not have it pre-installed, most notably Windows. The same holds for Glade, the RAD tool for managing the equivalent of what is known as a GUI resource file in Windows (in GTK+ it's a plain XML file, usually created and graphically managed by Glade).

Fortunately, it's quite easy to install everything needed, by downloading convenient bundles containing Windows binaries (although they are a bit behind in development, compared to their Unix/Linux counterparts). Further below in this text I point you to the right direction & places for getting and setting up everything you need to compile the game (or any other GTK+ application) on Windows. It's far more easy than it may look when reading my instructions, because I try to make things clear even to people who have no or very little experience.

In any case, if you have no experience with this kind of stuff, I would recommend to install the cross-platform Code::Blocks IDE, which comes with the MinGW32 GCC toolchain embed in a Windows specific installer (more on this later).

As far as the source code is concerned, regarding the GUI, all graphical elements of the game are stored in the file: gui\gtk_tic_en.glade (obviously you need Glade (version 3.x) to open it, in case you want to take a look at it... you may open it with any text editor though, since it's just an XML file).

All those graphical elements are loaded into memory via the function: gui_init(). This function initializes the GTK+ API and then it loads the needed graphical elements into the Gui environment of the game, by reading the glade file via a local GtkBuilder object. This GtkBuilder object is unreferenced just before the function returns control back to the main() function of the program.

Actually this function serves as a convenient abstraction for loading GUI elements, making it a bit more easy to port the program into a different GUI. That being said, the Gui environment itself is far from being properly abstracted, because I didn't took the time to implement all the needed functions for such a task. There are quite a few GTK+ specific functions that are called directly in several places (for example, to activate/de-activate menu items) which should be abstracted into more generic functions. Most probably I will finish it up in next versions.

As a final note about the GUI, there is a second glade file, called: gtk_tic_el.glade inside the \gui folder. This is the Greek translation of the English file, but Greek is not properly supported in the source code just yet (there are a couple of references inside the code, but they are actually disabled).

The reason is that currently I'm brutally updating some text labels of the GUI from within the source code, using standard string-handling functions, such sprintf(), strncpy() etc. Some of those functions are not getting along with UTF8 encoded strings, since they are primarily designed to work with 8-bit ASCII codes. Now, the Pango library used by GTK+ for text formatting on the GUI, expects valid UTF8 encoded strings (7-bit ASCII is valid UTF8, 8-bit is not).

To make a long story short, the most viable fix for supporting Greek along with English is to put the strings of both languages into the glade file (most probably in a hidden object) load them into my Gui environment and then use them from there whenever I need to dynamically update a GUI text label.

I'm planning to do so in next version of the game. In the meanwhile, you may rename the file: gtk_tic_el.glade into gtk_tic_en.glade if you want most of the GUI show up in Greek (make sure you keep a backup of the file: gtk_tic_en.glade first). Of course the dynamically updated GUI strings will still show up in English.


Console Output
--------------

The game does utilize a GUI but it also uses the console to mirror its output. By default all GTK+ applications open a console window, along with the main window of the application. It is left to the application whether to use the console or not. Tic-Tac-Toe Overkill uses it (GTK+ uses the console for outputting error messages, which can prove quite handy for end users too).

If you don't like this kind of behaviour, then add the GCC command line flag: -mwindows when compiling the source code. This will prevent the console window from being opened.

However, when in Human vs CPU mode, the game clears the board as soon as any of the players wins the current play (round). By having the console window open, you will be able to actually see what the winning combo was, before the board got cleared.

And that's the end of this section. In the rest of the file I will show you how to setup the GTK+ environment on Windows, including Glade, and I will show you how to compile GTK+ applications with the most popular free compilers & IDEs on a variety of platforms (mostly on Windows, though).



=============================================================================
I N S T A L L I N G G T K +
==============================================================================

GTK+ comes pre-installed on most Unix/Linux distributions, so here I will only explain briefly how to install it on systems running Windows 2000 or newer.

If your Unix/Linux installation does not include GTK+, please visit the official GTK+ site for instructions on how to download and/or build it.

The most recent versions of GTK+ at the moment of this writing were:

ver 3.2 for GNU/Linux/Unix:
http://www.gtk.org/download/linux.php

ver 2.24.8 for Win32 (for for the all-in-one-bundle):
http://www.gtk.org/download/win32.php

ver 2.22 for Win64 (experimental using MinGW-w64)
http://www.gtk.org/download/win64.php

for Mac OSX:
http://www.gtk.org/download/macos.php

For the always most recent versions of GTK+, visit the downloads page of the official GTK+ site:

http://www.gtk.org/download/


Setting up the GTK+ All-In-One Bundle on Windows
------------------------------------------------

If you intend to develop GTK+ applications (and of course being able to compile the source code of the TicTacToe Overkill) you need the GTK+ developer packages.

GTK+ depends on a few other libraries (namely: glib, atk, pango and gdk-pixbuf) so unless you already have them installed on your system, you must download and set them up too (Windows do NOT have them by default).

Fortunately, the GTK+ project guys release very convenient all-in-one bundles, containing everything, arranged in the correct folder-tree too!

Ok, here are the 3 simple steps to follow...

1. The first thing to do is to download the latest all-in-one-bundle
(see the links above) and extract it into any folder you like.
Personally, I use C:\unix\gtk+ but most people use C:\GTK2
Your are completely free to choose any folder you wish, but for
the shake of this text I'll assume you extracted it into:
C:\GTK2

2. Next you need to create the following Windows environment variables:

GTK_HOME = C:\GTK2
PKG_CONFIG_PATH = C:\GTK2\LIB\pkgconfig

(replace C:\GTK2 with the folder into which you extracted the
all-in-one-bundle)

NOTE: *** If any of the above environment variables already ***
*** exist on your system, do NOT delete their previous ***
*** contents. Simply add the above values, separating ***
*** them from the old ones with a semi-colon: ; ***
*** Preferably, add the new values BEFORE the old ones ***

3. The last step is to add GTK+2's binaries folder: C:\GTK2\BIN into
your Windows' PATH environment variable.

Again, do NOT delete any previous contents of your PATH. Simply add
C:\GTK2\BIN to the already existing contents, separating it with a
semi-colon character: ;


In the rare case you do not know how to create/modify environment variables on Windows, right-click on My Computer icon, then select Properties, and in the Advance tab select Environment variables:

My Computer (right-click) -> Properties -> [Advanced | Environment vars..]

You'll see 2 lists of environment variables: User and System. To have your modifications affect all users on your machine, do them in the System list of variables. To affect only the current user (advisable), do your modifications to the User list of variables.

To give you a better hint, the following screenshot shows my own setup:

http://img811.imageshack.us/img811/3703/environ.jpg



Setting up the GTK+ Runtime Environment on Windows
--------------------------------------------------

If you are only interested in running GTK+ application on Windows (that is, you don't care about developing GTK+ applications) then you need just the GTK+ runtime installer, which can be downloaded from the following link:

http://sourceforge.net/projects/gtk-win/

It is advertised as setting-up everything for you, with an absolutely minimal effort from your part, but please note that I have NO personal experience with it (I always use the all-in-one bundles, that contain everything).

Feel free to explore the relative wiki, at the following address:

http://sourceforge.net/userapps/mediawi ... =Main_Page




==============================================================================
I N S T A L L I N G G L A D E - 3
==============================================================================

Glade 3 is the de-facto, cross-platform, rapid development tool for designing GUI layouts, for applications developed with GTK+.

TIC-TAC-TOE Overkill comes with its own Glade-3 file, containing all its GUI elements (although some text labels are updated brutally, using the standard C library from within the source code). The file is called: GTK_TIC_EN.GLADE and it is located in the folder: GUI, along with the bitmap files used in the game.

As with all Glade-3 files, this is a plain XML file, but in order to browse/manage it graphically you need to open it with Glade-3. For more information check the Glade official site, at the following address: http://glade.gnome.org/

The Unix/Linux binaries of Glade-3 are most probably pre-installed on your distribution, along with at least the GTK+2 runtime binaries. But if you haven't updated your Unix/Linux recently, you may wish to do so in order to get the latest versions of both GTK+ and Glade (3.2 and 3.10, respectively). For Windows, the latest Glade version is 3.8.1, and you have to install it yourselves.

Note however, that this game only needs GTK+2. GTK+3 is advertised as backward compatible with GTK+2, but I haven't found the time to play with it yet, since I'm mostly working on Windows and there is no stable Windows GTK+3 yet.


Setting up Glade-3 on Windows
-----------------------------

***********************************************************************************
*** DO NOT USE the Glade-3 Windows Installer, not even form the official site ***
***********************************************************************************

The reason is that it contains a NASTY bug which will ERASE your Windows PATH environment variable. Thankfully not the one corresponding to your system environment, but it does erase the one corresponding to the environment of the current user.

Instead of the Windows installer, download the Glade 3.8.1 Win32 Binaries Bundle from this address (1st in the list):

http://www.optionexplicit.be/projects/g ... /20111123/

or if for some reason the above link does not work for you, try this direct link:
http://www.optionexplicit.be/projects/g ... _win32.zip

The bundle DOES NOT need installation! Just extract it to any folder you like on your system (I use C:\UNIX\GLADE-3) and then add its binaries folder into the PATH environment variable of your windows. And that's all about it!

So, if for example you extracted the bundle into the folder: C:\GLADE-3, you need to add the folder: C:\GLADE-3\BIN into the PATH environment variable of Windows (I have already explained how to add/delete/modify Windows environment variables, in the section: "Setting up the GTK+ All-In-One Bundle on Windows").

To run Glade-3, just double-click its executable, located inside its binaries folder.




==============================================================================
C O M P I L I N G G T K _ T I C . C
==============================================================================


On Unix/Linux & Windows with Cygwin/MinGW32/MinGW-w64/GnuWin
------------------------------------------------------------------------------

Provided GTK+2 is already installed on your system, you can compile the source code by typing the following command line:

gcc gtk_tic.c -o gtk_tic.exe `pkg-config --cflags --libs gtk+-2.0`

NOTE: *** The backquotes in the line above are NOT single quotes !!! ***
*** If you are on Windows, use either MinGW32's MSYS or Cygwin's ***
*** mintty terminal, or GnuWin's shell, because cmd.exe does NOT ***
*** understand backquotes. ***
*** Also make sure that the backquoted part is the LAST one in ***
*** the command line! ***


On Unix/Linux & Windows using Code::Blocks
------------------------------------------------------------------------------

Provided GTK+2 is already installed on your system, you can use the excellent, cross-platform Code::Blocks IDE, which is a perfect match for GCC on all platforms (on Windows it even comes with an alternative installer which besides the IDE it installs and sets-up MinGW32 too, automatically).

Code::Blocks is GTK+ ready, meaning that it provides a project wizard for GTK+ applications. All you have to do is to provide the folder of GTK+ on your system ONCE and that's all about it!

Actually there is another little thing you must do BEFORE EVERYTHING ELSE: make Code::Blocks's GTK+ wizard being aware of a change made in GTK+2 that differentiated the gdk_pixbuf library.

Visit this link:
http://www.gtkforums.com/viewtopic.php?t=8908

for nicely put, short, instructions. But do NOT download the GTK+2 bundle from that link, it is only for Windows and it's an old version too. For the most recent versions of GTK+ visit the download page at the official GTK+ site:

http://www.gtk.org/download/


On Windows using CMD.EXE
------------------------------------------------------------------------------

Provided GTK+2 and a Windows port of GCC are already installed and work on your system, you can compile the game (and every other GTK+ program) directly from the Windows command prompt. It's a bit tedious, but I'll give you an elegant solution to make it much simpler.

Assuming that a) GTK+2 is installed in the folder: C:\GTK2, b) you have put the folder: C:\GTK2\BIN in the PATH environment variable of your Windows, and c) you have created an environment variable GTK_HOME = "C:\GTK2", you can compile any GTK+2 application by typing the following (very long) command line at the prompt:

gcc gtk_tic.c -o gtk_tic.exe -mms-bitfields -Ic:/gtk2/include/gtk-2.0 -Ic:/gtk2/lib/gtk-2.0/include -Ic:/gtk2/include/atk-1.0 -Ic:/gtk2/include/cairo -Ic:/gtk2/include/pango-1.0 -Ic:/gtk2/include/glib-2.0 -Ic:/gtk2/lib/glib-2.0/include -Ic:/gtk2/include -Ic:/gtk2/include/freetype2 -Ic:/gtk2/include/libpng14 -Lc:/gtk2/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl

Of course, that is too much to type every time. All this line does is to direct GCC into specific folders for finding header files & libraries needed by GTK+2.

To make your life a lot easier, you can create ONCE two Windows environment variables, say GTK2_CFLAGS and GTK2_LIBS (you may give them any name you want) with the following contents:

GTK2_CFLAGS = "-mms-bitfields -Ic:/gtk2/include/gtk-2.0 -Ic:/gtk2/lib/gtk-2.0/include -Ic:/gtk2/include/atk-1.0 -Ic:/gtk2/include/cairo -Ic:/gtk2/include/pango-1.0 -Ic:/gtk2/include/glib-2.0 -Ic:/gtk2/lib/glib-2.0/include -Ic:/gtk2/include -Ic:/gtk2/include/freetype2 -Ic:/gtk2/include/libpng14"

GTK2_LIBS = "-Lc:/gtk2/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl"

Now you can compile any GTK+2 application by typing the following command line at the prompt:

gcc gtk_tic.c -o gtk_tic.exe %GTK2_CFLAGS% %GTK2_LIBS%



On Windows using Pelles C 6.00
------------------------------------------------------------------------------

This requires a bit of work on your part inside the IDE of Pelles C. The idea is to let Pelles C know where the GTK+2 header files & libraries are located, along with specifying which GTK+2 libraries should be linked to your executable.

I'm gonna use screenshots to walk you through the process, but KEEP IN MIND that on my setup GTK+2 is installed in the folder: C:\UNIX\GTK+ (in the previous paragraphs of this text I was using as an example the folder: C:\GTK2 instead, so make sure you adjust the folder to your own GTK+2 installation).

1. Create a new "Win32 Console Program (EXE)" empty project from within
the Pelles C IDE (you are free to use any folder you like of course)...

http://img412.imageshack.us/img412/8430 ... roject.jpg

2. Add the GTK+2 binaries folder to Pelles C IDE, using the menu command:
Tools -> Options [Folders >> Executables] ...

http://img137.imageshack.us/img137/8216 ... olsbin.jpg

3. Add all other GTK+2 dependencies into your project, using the menu command:
Project -> Project options ...

a) The [Compiler] tab ...

http://img684.imageshack.us/img684/940/ ... mpiler.jpg

b) The [Linker] tab...

http://img249.imageshack.us/img249/3875 ... linker.jpg

To save you some typing (lots of it) here is the complete string
to paste right after "delayimp.lib" in the above screenshot (make
sure you type an empty space before pasting):

gtk-win32-2.0.lib gdk-win32-2.0.lib atk-1.0.lib gio-2.0.lib gdk_pixbuf-2.0.lib pangowin32-1.0.lib gdi32.lib pangocairo-1.0.lib pango-1.0.lib cairo.lib gobject-2.0.lib gmodule-2.0.lib gthread-2.0.lib glib-2.0.lib intl.lib

c) The [Folders] tab...

First the Libraries:
http://img685.imageshack.us/img685/3450 ... ctlibs.jpg

Then the Includes:
http://img80.imageshack.us/img80/9430/p ... nclude.jpg

To add and re-arrange the order of folders when you are in the
[Folders] tab, use the corresponding icons right next to the
drop-down list.

4. Save your project, hit the Build & Run button and you are all done!

By following the above steps you can compile, build and run the gtk_tic game from within Pelles C IDE, on Windows.

Unfortunately the GUI project manager of Pelles C does NOT allow project cloning, so it's not trivial to make a generic GTK+2 project and then graphically clone it for every new GTK+2 application you develop. Making a GTK+ project wizard is not trivial either, although it is ideal as a permanent solution (check the Pelles C SDK for working samples on creating project wizards).

You can still clone manually your project's folder using the Windows Explorer, but you must also rename/delete files inside that folder, and most importantly you should manually edit the *.ppj file to match your new project's naming. And of course you must add/remove source files to your new project, once you load it inside the IDE.

The above is equivalent to using Pelles C's make utility from the command line (it is called POMAKE.EXE) along with its /f switch, for building executables through any *.ppj file, but setting that up is out of the scope for this text!



==============================================================================
L I N K S O F R E F E R E N C E D S O F T W A R E
==============================================================================

Here is a list of links to the sites of all the software I have referenced in this text, in alphabetical order...

Code::Blocks IDE : http://www.codeblocks.org/
the Cygwin project : http://www.cygwin.com/
Glade (a User Interface Designer) : http://glade.gnome.org/
GnuWin (former GnuWin32) : http://gnuwin32.sourceforge.net/
the GTK+ project : http://www.gtk.org/
the MinGW32 project : http://www.mingw.org/
the MinGW-w64 project : http://mingw-w64.sourceforge.net/
Pelles C : http://www.smorgasbordet.com/pellesc/


Πείτε μου αν όλα δουλεύουν καλώς.
Τελευταία επεξεργασία από migf1 και 24 Ιούλ 2012, 16:26, έχει επεξεργασθεί 10 φορά/ες συνολικά
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό medigeek » 03 Ιαν 2012, 11:34

migf1 έγραψε:Πείτε μου αν όλα δουλεύουν καλώς.

* Ο σύνδεσμος στο box.com αναφέρεται σε άλλο πρόγραμμα νομίζω (battle;)
* Η άδεια χρήσης; Σύμφωνα με αυτό που έβαλες, συστήνω MIT ή GPLv3. :)
* LICENSE : No License! Consider it freeware, public domain or whatever, and do
* whatever you want with it. All I ask is to reference my name and
* include the original package of the game if you decide to redistribute
* it. Oh, I'll be happy to see it ported or improved, so drop me a note
* if you feel like it :)
Κύπριος; Κόπιασε στο ubuntu-cy! ┃ Launchpad Debian Github
Οδηγός για νεοεισερχόμενους -- Αρχικές οδηγίες για αρχάριους χρήστες του Ubuntu

1 Γνώσεις Linux: Πολύ καλό ┃ Προγραμματισμού: Πολύ καλό ┃ Αγγλικών: Πολύ καλό
2 Ubuntu 12.10 quantal 3.5.0-21-generic 64bit (en_US.UTF-8, GNOME cinnamon2d), Ubuntu 3.5.0-19-generic, Windows 7
3 Intel Core2 Duo CPU E6550 2.33GHz ‖ RAM 5970 MiB ‖ MSI MS-7235
4 nVidia G73 [GeForce 7300 GT] [10de:0393] {nvidia}
5 eth0: Realtek RTL-8110SC/8169SC Gigabit Ethernet [10ec:8167] (rev 10)
Άβαταρ μέλους
medigeek
Freedom
Freedom
 
Δημοσιεύσεις: 5023
Εγγραφή: 24 Μάιος 2008, 14:49
Τοποθεσία: Σερβία/Κύπρος
Launchpad: medigeek
IRC: savvas
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό migf1 » 03 Ιαν 2012, 11:42

Καλημέρα & χρόνια πολλά, ευχαριστώ για την επισήμανση! Το διόρθωσα το link.
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό Star_Light » 03 Ιαν 2012, 14:52

migf1 φοβερο!!!!! Ειδικα τα γραφικα στην τριλιζα.... σε C το έγραψες επειδη καπου ειδα οτι λες για OOP χρησιμοποιησες και C++ καθολου?
αν και νομιζω η βιβλιοθηκη για τα γραφικα μαλλον σε C++ θα ειναι γιατι η C δεν μπορει να "σηκωσει" γραφικο περιβαλλον.

Παρολαυτά φαινεται τρομερό
Γνώσεις ⇛ Linux: Βασικές ┃ Προγραμματισμός: Δέν θέλω μεροκάματο , θέλω C και κακο θάνατο! ┃ Αγγλικά: Lower
Λειτουργικό ⇛ Ubuntu 10.10 σε Dual Boot με Windows 7
Προδιαγραφές ⇛ Επεξεργαστής : Intel(R) Core(TM) i3 CPU 540 @3.07Ghz (64bit)
RAM : Kingston 2GB
HDD : Coreshare 500GB
Κάρτα Γραφικών : Intel Corporation Core Processor Integrated Graphics Controller(rev 18) (prog-if 00 [VGA controller]) [8086:0042]
Star_Light
superbTUX
superbTUX
 
Δημοσιεύσεις: 2787
Εγγραφή: 01 Μάιος 2010, 21:07
Τοποθεσία: Αθήνα
IRC: Star_Light
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό migf1 » 03 Ιαν 2012, 16:13

Star_Light έγραψε:migf1 φοβερο!!!!! Ειδικα τα γραφικα στην τριλιζα.... σε C το έγραψες επειδη καπου ειδα οτι λες για OOP χρησιμοποιησες και C++ καθολου?
αν και νομιζω η βιβλιοθηκη για τα γραφικα μαλλον σε C++ θα ειναι γιατι η C δεν μπορει να "σηκωσει" γραφικο περιβαλλον.

Παρολαυτά φαινεται τρομερό


Ευχαριστώ!

Καθαρή C, τίποτε άλλο (αφού έχουμε πως με C μπορείς να κάνεις τα πάντα, άλλα πιο εύκολα, άλλα πιο δύσκολα :)
Το GTK+ είναι γραμμένο αποκλειστικά σε C και είναι καθαρά Object Oriented framework, βασισμένο πάνω στο GObject της glib.

OOP είναι και το abstraction του κώδικα που έγραψα, η προσέγγιση δηλαδή στην επίλυση του ζητούμενου. Κατά βαση, ως φιλοσοφία δηλαδή, τα περιβάλλοντα Game, Gui, GamePlay είναι abstract objects, παρόλο που δεν κάθισα να υλοποιήσω abstract constructors/deconstructors, public & private μεθόδους (member functions) κλπ για το καθένα τους... γιατί δεν είχε και πολύ νόημα σε αυτό το μικρούλι πρόγραμμα.

Μπορείς για παράδειγμα να φτιάξεις κάτι τέτοιο σε ξεχωριστό source module, π.χ. animal.h (σε spoiler)...

Spoiler: show
Κώδικας: Επιλογή όλων
    #enum { MAMAL=0 /* rest of animal types here */ };

    typedef struct Animal Animal;
    struct Animal {
        /* public data */
        int  type;
        int  age;
        ...
        /* public methods */
        Bool (*SetType)( Animal *self, int type );
        Bool (*SetAge)( Animal *self, int age );
        int (*GetType)( Animal *self );
        int (*GetAge)( Animal *self );
        ...
        /* private data & methods */
        ...
  };

...
/* ---------------------------------------------- */
int animal_get_type( Animal *self )
{
    /* do some error checking here ... */

    return self->type;
}
/* ---------------------------------------------- */
int animal_get_age( Animal *self )
{
    /* do some error checking here ... */

    return self->age;
}
/* ---------------------------------------------- */
Bool animal_set_type( Animal *self, int type )
{
    if ( !self || INVALID(type) )
        return FALSE;

    self->type = type;
    return TRUE;
}
/* ---------------------------------------------- */
Bool animal_set_age( Animal *self, int age )
{
    if ( !self || INVALID(type) )
        return FALSE;

    self->type = age;
    return TRUE;
}
/* ---------------------------------------------- */
void freeAnimal( Animal *self )
{
    if ( self )
        free( self);
    return;
}
/* ---------------------------------------------- */
Animal *newAnimal()
{
    Animal *self = calloc( sizeof(Animal) );
    if ( self )
        return NULL;

    /* give initial (default) values to public data */
    self->type = MAMAL;
    self->age = 0;
    ...

    /* initialize public methods */
    self->SetType = &animal_set_type;
    self->SetAge = &animal_set_age;
    self->GetType = &animal_get_type;
    self->GetAge = &animal_get_age;
    ...

    /* initialize private data & methods here ... */

    return self;
}

και κατόπιν στο source module της main() να γράφεις OO κώδικα όπως ο παρακάτω...
Κώδικας: Επιλογή όλων
#include "animal.h"
...
int main( void )
{
    Animal *dog = newAnimal();
    Animal *cat = newAnimal();

    dog.SetType( dog, MAMAL);
    dog.SetAge( dog, 2 );
    ...
    printf("the cat is %d years old\n", cat.GetAge(cat) );
    printf("the dog is of type %d\n", dog.GetType(dog) );
    ...
    freeAnimal( dog );
    freeAnimal( cat );
    return 0;
}

Στον κώδικα της τρίλιζας εγώ δεν ασχολήθηκα με ξεχωριστές υλοποιήσεις κλάσεων κλπ, απλώς μπορείς να θεωρήσεις πως τα Game, GamePlay, Gui είναι abstract objects (θα μπορούσαν να θεωρηθούν και κλάσεις αν το είχα έτσι ώστε να "γεννούν" objects). Πάντως κι έτσι όπως τον έχω τον κώδικα, μπορεί κάλλιστα να το θεωρήσει κανείς overkill (εξού και το όνομά του, :lol:)

Για την εικόνα τώρα, απλά την πήρα από την Wikipedia: http://nrm.wikipedia.org/wiki/File:Jogo ... ac_toe.png, τις αφαίρεσα ότι δεν ήθελα (αν δείτε στις ενώσεις του πλέγματος φαίνεται πως εκεί υπήρχε γραμμή στην αριστερή διαγώνιο) και την έσπασα σε κομμάτια, που τα χρησιμοποιώ στο παιχνίδι. Αυτά τα κομμάτια είναι απλές .png εικόνες, και βρίσκονται μέσα στον φάκελο /gui του παιχνιδιού.

ΥΓ. Τελικά το δοκίμασε κανείς σε Linux να μου πει αν είναι όλα OK;
Τελευταία επεξεργασία από migf1 και 03 Ιαν 2012, 16:22, έχει επεξεργασθεί 1 φορά/ες συνολικά
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό Ilias95 » 03 Ιαν 2012, 16:22

migf1 έγραψε:ΥΓ. Τελικά το δοκίμασε κανείς σε Linux να μου πει αν είναι όλα OK;

Αν κατάλαβα καλά και είναι αυτή η εντολή για το compile:
Κώδικας: Επιλογή όλων
ilias@ilias-pc:~/Έγγραφα/gtk_tic$ ls
gtk_tic.c  gtk_tic.exe  gtk_tic_no_console.exe  gui  readme.txt
ilias@ilias-pc:~/Έγγραφα/gtk_tic$ gcc gtk_tic.c -o gtk_tic.exe `pkg-config --cflags --libs gtk+-2.0`
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
gtk_tic.c:32:21: μοιραίο σφάλμα: gtk/gtk.h: Δεν υπάρχει τέτοιο αρχείο ή κατάλογος
compilation terminated.
ilias@ilias-pc:~/Έγγραφα/gtk_tic$

Λογικά κάνω κάτι λάθος εγώ;
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό migf1 » 03 Ιαν 2012, 16:25

Ilias95 έγραψε:
migf1 έγραψε:ΥΓ. Τελικά το δοκίμασε κανείς σε Linux να μου πει αν είναι όλα OK;

Αν κατάλαβα καλά και είναι αυτή η εντολή για το compile:
Κώδικας: Επιλογή όλων
ilias@ilias-pc:~/Έγγραφα/gtk_tic$ ls
gtk_tic.c  gtk_tic.exe  gtk_tic_no_console.exe  gui  readme.txt
ilias@ilias-pc:~/Έγγραφα/gtk_tic$ gcc gtk_tic.c -o gtk_tic.exe `pkg-config --cflags --libs gtk+-2.0`
Package gtk+-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-2.0' found
gtk_tic.c:32:21: μοιραίο σφάλμα: gtk/gtk.h: Δεν υπάρχει τέτοιο αρχείο ή κατάλογος
compilation terminated.
ilias@ilias-pc:~/Έγγραφα/gtk_tic$

Λογικά κάνω κάτι λάθος εγώ;

Μήπως δεν είναι εγκατεστημένο το GTK+ στο σύστημά σου; Η εντολή είναι η σωστή!

Για τσέκαρε τις μεταβλητές περιβάλλοντος: GTK_HOME και PKG_CONFIG_PATH τι τιμές έχουν;
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό Ilias95 » 03 Ιαν 2012, 16:30

Αν κατάλαβα σωστά πάλι:
Κώδικας: Επιλογή όλων
ilias@ilias-pc:~$ echo $GTK_HOME

ilias@ilias-pc:~$ echo $PKG_CONFIG_PATH

ilias@ilias-pc:~$
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό migf1 » 03 Ιαν 2012, 16:36

Χμ, για γράψε: gtk-demo

Σου ανοίγει το demo application του GTK+ ?
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: C, GTK+2: Crossplatform Τρίλιζα (Tic-Tac-Toe Overkill)

Δημοσίευσηαπό Ilias95 » 03 Ιαν 2012, 16:40

Κώδικας: Επιλογή όλων
ilias@ilias-pc:~$ gtk-demo
Το πρόγραμμα 'gtk-demo' δεν είναι αυτή τη στιγμή εγκατεστημένο.  Μπορείτε να το εγκαταστήσετε πληκτρολογώντας:
sudo apt-get install gtk2.0-examples

Μετά το εγκατέστησα και έτρεξε κανονικά.
Μάλλον δεν έχω εγκατεστημένο το gtk αλλά δεν μπορώ να βρω πιο πακέτο πρέπει να εγκαταστήσω.
Εφαρμογές με gtk σε python όμως μπορώ να γράψω κανονικά.
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Επόμενο

Επιστροφή στο Ανάπτυξη Λογισμικού / Αλγόριθμοι

cron