From 78293905481ab7a67e773d05350da29940a58ea6 Mon Sep 17 00:00:00 2001
From: Sam Moore <matches@ucc.asn.au>
Date: Fri, 23 Dec 2011 12:30:38 +0800
Subject: [PATCH] Added build option to build "stratego" without graphics

I need this for mufasa (vm), which doesn't have SDL or OpenGL or even GNOME
Besides, it doesn't need it, and attempting to install them did not end well...

Still need to test that it actually builds on mufasa
---
 judge/manager/game.cpp             | 23 ++++++++++++++++++++---
 judge/manager/game.h               |  2 +-
 judge/manager/graphics.cpp         | 11 +++++++----
 judge/manager/graphics.h           | 20 +++++++++++++++-----
 judge/manager/human_controller.cpp |  5 ++++-
 judge/manager/main.cpp             |  7 +++++++
 judge/manager/stratego.cpp         | 11 +++++------
 judge/manager/stratego.h           | 11 ++++++-----
 web/doc/manager_manual.txt         | 22 +++++++++++++++++++++-
 9 files changed, 86 insertions(+), 26 deletions(-)

diff --git a/judge/manager/game.cpp b/judge/manager/game.cpp
index 3d6dcc8..df7a061 100644
--- a/judge/manager/game.cpp
+++ b/judge/manager/game.cpp
@@ -1,5 +1,5 @@
 #include "game.h"
-
+#include <stdarg.h>
 using namespace std;
 
 
@@ -20,8 +20,10 @@ Game::Game(const char * redPath, const char * bluePath, const bool enableGraphic
 	signal(SIGPIPE, Game::HandleBrokenPipe);
 
 
+	#ifdef BUILD_GRAPHICS
 	if (graphicsEnabled && (!Graphics::Initialised()))
 			Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
+	#endif //BUILD_GRAPHICS
 
 	if (strcmp(redPath, "human") == 0)
 		red = new Human_Controller(Piece::RED, graphicsEnabled);
@@ -49,9 +51,10 @@ Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime
 	Game::theGame = this;
 	signal(SIGPIPE, Game::HandleBrokenPipe);
 
-
+	#ifdef BUILD_GRAPHICS
 	if (graphicsEnabled && (!Graphics::Initialised()))
 			Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
+	#endif //BUILD_GRAPHICS
 
 	input = fopen(fromFile, "r");
 
@@ -201,16 +204,18 @@ void Game::Wait(double wait)
 	TimerThread timer(wait*1000000); //Wait in seconds
 	timer.Start();
 
+	#ifdef BUILD_GRAPHICS
 	if (!graphicsEnabled)
 	{
 		while (!timer.Finished());
 		timer.Stop();
 		return;
 	}
-
+	#endif //BUILD_GRAPHICS
 
 	while (!timer.Finished())
 	{
+		#ifdef BUILD_GRAPHICS
 		SDL_Event  event;
 		while (SDL_PollEvent(&event))
 		{
@@ -222,6 +227,7 @@ void Game::Wait(double wait)
 					break;
 			}
 		}
+		#endif //BUILD_GRAPHICS
 	}
 	timer.Stop();
 	
@@ -256,6 +262,8 @@ void Game::HandleBrokenPipe(int sig)
 	if (Game::theGame->printBoard)
 		Game::theGame->theBoard.PrintPretty(stdout, Piece::BOTH);
 
+	
+	#ifdef BUILD_GRAPHICS
 	if (Game::theGame->graphicsEnabled && theGame->log == stdout)
 	{
 		theGame->logMessage("CLOSE WINDOW TO EXIT\n");
@@ -275,6 +283,7 @@ void Game::HandleBrokenPipe(int sig)
 		}
 	}
 	else
+	#endif //BUILD_GRAPHICS
 	{
 		if (theGame->log == stdout)
 		{
@@ -396,6 +405,8 @@ void Game::PrintEndMessage(const MovementResult & result)
 		theBoard.PrintPretty(stdout, Piece::BOTH);
 		fprintf(stdout, "\n");
 	}
+
+	#ifdef BUILD_GRAPHICS
 	if (graphicsEnabled && log == stdout)
 	{
 		logMessage("CLOSE WINDOW TO EXIT\n");
@@ -415,6 +426,7 @@ void Game::PrintEndMessage(const MovementResult & result)
 		}
 	}
 	else
+	#endif //BUILD_GRAPHICS
 	{
 		if (log == stdout)
 		{
@@ -459,8 +471,10 @@ MovementResult Game::Play()
 			fprintf(stdout, "\n\n");
 		}
 
+		#ifdef BUILD_GRAPHICS
 		if (graphicsEnabled)
 			theBoard.Draw(toReveal);
+		#endif //BUILD_GRAPHICS
 		
 		turn = Piece::RED;
 		logMessage( "%d RED: ", turnCount);
@@ -485,8 +499,11 @@ MovementResult Game::Play()
 			theBoard.PrintPretty(stdout, toReveal);
 			fprintf(stdout, "\n\n");
 		}
+		
+		#ifdef BUILD_GRAPHICS
 		if (graphicsEnabled)
 			theBoard.Draw(toReveal);
+		#endif //BUILD_GRAPHICS
 
 		
 		
diff --git a/judge/manager/game.h b/judge/manager/game.h
index 1212b89..43f4d0e 100644
--- a/judge/manager/game.h
+++ b/judge/manager/game.h
@@ -4,7 +4,7 @@
 #include "stratego.h"
 #include "ai_controller.h"
 #include "human_controller.h"
-
+#include <cstring>
 
 
 /**
diff --git a/judge/manager/graphics.cpp b/judge/manager/graphics.cpp
index 5b708df..7cc22d0 100644
--- a/judge/manager/graphics.cpp
+++ b/judge/manager/graphics.cpp
@@ -3,6 +3,7 @@
 #include <cassert>
 #include <iostream>
 
+#ifdef BUILD_GRAPHICS
 
 #undef DEBUG
 //#define DEBUG
@@ -127,6 +128,7 @@ void Texture::Draw(int x, int y, double angle , double scale )
 }
 
 
+
 Font::Font(const char * filename, int newWidth, int newHeight) : Texture(filename), width(newWidth), height(newHeight)
 {
 
@@ -442,8 +444,9 @@ Colour Graphics::ConvertColour(Uint8 from)
 	return result;
 }
 
+void Graphics::Wait(int n)
+{
+	SDL_Delay(n);
+}
 
-
-
-
-
+#endif //BUILD_GRAPHICS
diff --git a/judge/manager/graphics.h b/judge/manager/graphics.h
index f81bdb3..2a96932 100644
--- a/judge/manager/graphics.h
+++ b/judge/manager/graphics.h
@@ -1,19 +1,26 @@
+//#define BUILD_GRAPHICS
+#ifdef BUILD_GRAPHICS
+
 #ifndef GRAPHICS_H
 #define GRAPHICS_H
 
+
 #include <SDL/SDL.h>
 #include <SDL/SDL_opengl.h>
 
+typedef SDL_Surface Screen;
+typedef SDL_Rect Rectangle;
+typedef short unsigned int SUint;
+typedef unsigned char Uint8;
 
 #include <vector>
 #include <list>
 
 
-typedef SDL_Surface Screen;
-typedef SDL_Rect Rectangle;
 
 
-typedef short unsigned int SUint;
+
+
 
 class Texture;
 class Font;
@@ -72,8 +79,8 @@ class Graphics
 
 		static SDL_Surface * TextureFromColours(std::vector<SUint> * R, std::vector<SUint> * G, std::vector<SUint> * B, std::vector<SUint> * A = NULL);
 		static SDL_Surface * TextureFromColours(std::vector<std::vector<SUint> > * R, std::vector<std::vector<SUint> > * G, std::vector<std::vector<SUint> > * B, std::vector<std::vector<SUint> > * A = NULL);
-
-		static void Wait(int n) {SDL_Delay(n);}
+		
+		static void Wait(int n);
 
 		template <class T>
 		class TextureManager
@@ -120,6 +127,7 @@ class Texture
 		
 		int width() const {return surface->w;}
 		int height() const {return surface->h;}
+
 	protected:
 		SDL_Surface * surface;
 		GLuint texture;
@@ -145,4 +153,6 @@ class Font : private Texture
 
 #endif //GRAPHICS_H
 
+#endif //BUILD_GRAPHICS
+
 //EOF
diff --git a/judge/manager/human_controller.cpp b/judge/manager/human_controller.cpp
index bf43e8d..c2470d3 100644
--- a/judge/manager/human_controller.cpp
+++ b/judge/manager/human_controller.cpp
@@ -110,9 +110,10 @@ MovementResult Human_Controller::QueryMove(string & buffer)
 	}
 
 	
-
+	#ifdef BUILD_GRAPHICS
 	if (graphicsEnabled)
 	{
+		
 		fprintf(stdout, "Click to move!\n");
 		SDL_Event event; int mouseClick = 0;
 
@@ -164,8 +165,10 @@ MovementResult Human_Controller::QueryMove(string & buffer)
 			}
 		}
 		fprintf(stdout, "Move complete!\n");
+		
 	}
 	else
+	#endif //BUILD_GRAPHICS
 	{
 		buffer.clear();
 		for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin))
diff --git a/judge/manager/main.cpp b/judge/manager/main.cpp
index 0fbd6ae..9d2cad1 100644
--- a/judge/manager/main.cpp
+++ b/judge/manager/main.cpp
@@ -8,6 +8,7 @@
 
 #include "game.h"
 
+
 using namespace std;
 
 Piece::Colour SetupGame(int argc, char ** argv);
@@ -82,7 +83,13 @@ Piece::Colour SetupGame(int argc, char ** argv)
 					++ii;
 					break;
 				case 'g':
+					#ifdef BUILD_GRAPHICS
 					graphics = !graphics;
+					#else
+					fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
+					exit(EXIT_FAILURE);
+					#endif //BUILD_GRAPHICS
+
 					break;
 				case 'p':
 					printBoard = !printBoard;
diff --git a/judge/manager/stratego.cpp b/judge/manager/stratego.cpp
index 410b70a..e7cf60a 100644
--- a/judge/manager/stratego.cpp
+++ b/judge/manager/stratego.cpp
@@ -14,12 +14,9 @@ int Piece::maxUnits[] = {0,0,1,1,8,5,4,4,4,3,2,1,1,6,0};
 
 
 
-
+#ifdef BUILD_GRAPHICS
 Piece::TextureManager Piece::textures;
 
-
-
-
 Piece::TextureManager::~TextureManager()
 {
 	Array<Texture*>::Iterator i(*this);
@@ -41,7 +38,7 @@ Texture & Piece::TextureManager::operator[](const LUint & at)
 	}
 	return *(Array<Texture*>::operator[](at));
 }
-
+#endif //BUILD_GRAPHICS
 
 /**
  * Gets the type of a piece, based off a character token
@@ -197,7 +194,7 @@ void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
 }
 
 
-
+#ifdef BUILD_GRAPHICS
 /**
  * Draw the board state to graphics
  * @param reveal - Pieces matching this colour will be revealed. If Piece::BOTH, all pieces will be revealed
@@ -206,6 +203,7 @@ void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
  */
 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
 {
+
 	if (!Graphics::Initialised())
 	{
 		fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
@@ -256,6 +254,7 @@ void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
 	Graphics::UpdateScreen();
 	
 }
+#endif //BUILD_GRAPHICS
 
 /**
  * Adds a piece to the board
diff --git a/judge/manager/stratego.h b/judge/manager/stratego.h
index fe73aa1..c8f4f9d 100644
--- a/judge/manager/stratego.h
+++ b/judge/manager/stratego.h
@@ -6,9 +6,8 @@
 
 #include <assert.h>
 
-
-	#include "graphics.h"
-	#include "array.h"
+#include "graphics.h"
+#include "array.h"
 
 #include <vector>
 
@@ -58,6 +57,7 @@ class Piece
 
 		public:
 
+			#ifdef BUILD_GRAPHICS
 			class TextureManager : public Graphics::TextureManager<LUint>, private Array<Texture*>
 			{
 				public:
@@ -86,7 +86,7 @@ class Piece
 						break;
 				}
 			}
-
+			#endif //BUILD_GRAPHICS
 			
 			
 
@@ -108,8 +108,9 @@ class Board
 		void Print(FILE * stream, const Piece::Colour & reveal=Piece::BOTH); //Print board
 		void PrintPretty(FILE * stream, const Piece::Colour & reveal=Piece::BOTH); //Print board using colour
 		
+		#ifdef BUILD_GRAPHICS
 		void Draw(const Piece::Colour & reveal=Piece::BOTH, bool showRevealed = true); //Draw board
-		
+		#endif //BUILD_GRAPHICS
 
 		bool AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour); //Add piece to board
 
diff --git a/web/doc/manager_manual.txt b/web/doc/manager_manual.txt
index 239c6ac..9024c74 100644
--- a/web/doc/manager_manual.txt
+++ b/web/doc/manager_manual.txt
@@ -36,6 +36,8 @@ A WARNING ABOUT BUFFERING
 	
 OPTIONS
 	-g
+		NOTE: This switch only functions if stratego is built with graphics enabled. See BUILDING for more information.
+
 		By default, graphics are disabled. If the -g switch is present, stratego will draw the game as it is played using SDL/OpenGL
 		
 	-p
@@ -216,6 +218,24 @@ EXIT/OUTPUT
 
 	If an error occurs within stratego itself, an error message will be printed to stderr and return exit code 1.
 	If possible, stratego will print the message "QUIT" to both AI programs, and they should exit as soon as possible.
+
+BUILDING
+	To build from source, simply run make in the source directory.
+
+	stratego can be built with or without graphics enabled. By default, graphics are disabled.
+
+	To enable graphics, ensure that the first line of the source file "graphics.h" reads:
+
+	#define BUILD_GRAPHICS
+
+	To disable graphics, comment out the first line of "graphics.h" i.e ensure that it reads:
+
+	//#define BUILD_GRAPHICS
+
+	If you intend to build with graphics enabled, you will need the SDL and OpenGL developement libraries installed first.
+	If you intend to use graphics, please ensure the "images" directory is located in the executable's run directory.
+
+	
 	
 
 BUGS	
@@ -242,5 +262,5 @@ NOTES
 	   irc://irc.ucc.asn.au #progcomp
 
 THIS PAGE LAST UPDATED
-	20/12/11 by Sam Moore
+	23/12/11 by Sam Moore
 	
-- 
GitLab