Could anyone break the MyWindow class out into separate MyWindow.h and MyWindow.cpp files from this excellent FLTK example?
I have tried all afternoon. I am stuck on the (void*)this part in the Fl::add_timeout method. I am sure I have other things wrong as well.
I would like to see how the recursion is best handled. Do you MyWindow::RenderImage_CB or leave it as RenderImage_CB?
Adding the code here for convenience.
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>
#include <stdio.h>
#include <time.h>
#define XSIZE 500
#define YSIZE 500
#define UPDATE_RATE 0.05 // update rate in seconds
// Demonstrate how to display a pixel buffer in FLTK
// WINDOW CLASS TO HANDLE DRAWING IMAGE
class MyWindow : public Fl_Double_Window {
unsigned char pixbuf[YSIZE][XSIZE][3]; // image buffer
// FLTK DRAW METHOD
void draw() {
fl_draw_image((const uchar*)&pixbuf, 0, 0, XSIZE, YSIZE, 3, XSIZE*3);
}
// TIMER CALLBACK: CALLED TO UPDATE THE DRAWING
static void RenderImage_CB(void *userdata) {
MyWindow *win = (MyWindow*)userdata;
win->RenderImage();
Fl::repeat_timeout(UPDATE_RATE, RenderImage_CB, userdata);
}
public:
// CTOR
MyWindow(int w, int h, const char *name=0) : Fl_Double_Window(w,h,name) {
end();
RenderImage(); // show first drawing
// Start timer updating
Fl::add_timeout(UPDATE_RATE, RenderImage_CB, (void*)this);
}
// PLOT A PIXEL AS AN RGB COLOR INTO THE PIXEL BUFFER
void PlotPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
pixbuf[y][x][0] = r;
pixbuf[y][x][1] = g;
pixbuf[y][x][2] = b;
}
// MAKE A NEW PICTURE IN THE PIXEL BUFFER, SCHEDULE FLTK TO DRAW IT
void RenderImage() {
static unsigned char drawcount = 0;
for ( int x=0; x<XSIZE; x++ )
for ( int y=0; y<YSIZE; y++ )
PlotPixel(x, y, x+drawcount, y+drawcount, x+y+drawcount);
++drawcount;
redraw();
}
};
int main(int argc, char**argv) {
Fl::visual(FL_RGB); // prevents dithering on some systems
MyWindow *win = new MyWindow(XSIZE, YSIZE);
win->show();
return(Fl::run());
}
Separated this way gives a compiler error:
main.cpp
#include "MyWindow.h"
int main(int argc, char**argv) {
Fl::visual(FL_RGB); // prevents dithering on some systems
MyWindow *win = new MyWindow(XSIZE, YSIZE);
win->show();
return(Fl::run());
}
MyWindow.h
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>
#include <stdio.h>
#include <time.h>
#define XSIZE 500
#define YSIZE 500
#define UPDATE_RATE 0.05 // update rate in seconds
// WINDOW CLASS TO HANDLE DRAWING IMAGE
class MyWindow : public Fl_Double_Window {
unsigned char pixbuf[YSIZE][XSIZE][3]; // image buffer
// FLTK DRAW METHOD
void draw();
// TIMER CALLBACK: CALLED TO UPDATE THE DRAWING
void RenderImage_CB(void *userdata);
public:
// CTOR
MyWindow(int w, int h, const char *name=0) : Fl_Double_Window(w,h,name){};
// PLOT A PIXEL AS AN RGB COLOR INTO THE PIXEL BUFFER
void PlotPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b);
// MAKE A NEW PICTURE IN THE PIXEL BUFFER, SCHEDULE FLTK TO DRAW IT
void RenderImage();
};
MyWindow.cpp
#include "MyWindow.h"
void MyWindow::draw() {
fl_draw_image((const uchar*)&pixbuf, 0, 0, XSIZE, YSIZE, 3, XSIZE*3);
}
void MyWindow::RenderImage_CB(void *userdata) {
MyWindow *win = (MyWindow*)userdata;
win->RenderImage();
Fl::repeat_timeout(UPDATE_RATE, MyWindow::RenderImage_CB, userdata);
}
MyWindow::MyWindow(int w, int h, const char *name) : Fl_Double_Window(w,h,name) {
end();
MyWindow::RenderImage(); // show first drawing
// Start timer updating
Fl::add_timeout(UPDATE_RATE, MyWindow::RenderImage_CB, (void*)this);
}
void MyWindow::PlotPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
pixbuf[y][x][0] = r;
pixbuf[y][x][1] = g;
pixbuf[y][x][2] = b;
}
void MyWindow::RenderImage() {
static unsigned char drawcount = 0;
for ( int x=0; x<XSIZE; x++ )
for ( int y=0; y<YSIZE; y++ )
PlotPixel(x, y, x+drawcount, y+drawcount, x+y+drawcount);
++drawcount;
redraw();
}
Using the above my first error is:
MyWindow.cpp:10:71: error: no matching function for call to 'Fl::repeat_timeout(double, <unresolved overloaded function type>, void*&)'
Fl::repeat_timeout(UPDATE_RATE, MyWindow::RenderImage_CB, userdata);
This compiles and runs:
main.cpp
#include "MyWindow.h"
int main(int argc, char**argv) {
Fl::visual(FL_RGB); // prevents dithering on some systems
MyWindow *win = new MyWindow(XSIZE, YSIZE);
win->show();
return(Fl::run());
}
MyWindow.h
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>
#include <stdio.h>
#include <time.h>
#define XSIZE 500
#define YSIZE 500
#define UPDATE_RATE 0.05 // update rate in seconds
// WINDOW CLASS TO HANDLE DRAWING IMAGE
class MyWindow : public Fl_Double_Window {
unsigned char pixbuf[YSIZE][XSIZE][3]; // image buffer
// FLTK DRAW METHOD
void draw();
// TIMER CALLBACK: CALLED TO UPDATE THE DRAWING
static void RenderImage_CB(void *userdata);
public:
// CTOR
MyWindow(int w, int h, const char *name=0);// : Fl_Double_Window(w,h,name){};
// PLOT A PIXEL AS AN RGB COLOR INTO THE PIXEL BUFFER
void PlotPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b);
// MAKE A NEW PICTURE IN THE PIXEL BUFFER, SCHEDULE FLTK TO DRAW IT
void RenderImage();
};
MyWindow.cpp
#include "MyWindow.h"
MyWindow::MyWindow(int w, int h, const char *name) : Fl_Double_Window(w,h,name) {
end();
MyWindow::RenderImage(); // show first drawing
// Start timer updating
Fl::add_timeout(UPDATE_RATE, MyWindow::RenderImage_CB, (void*)this);
}
void MyWindow::draw() {
fl_draw_image((const uchar*)&pixbuf, 0, 0, XSIZE, YSIZE, 3, XSIZE*3);
}
void MyWindow::RenderImage_CB(void *userdata) {
MyWindow *win = (MyWindow*)userdata;
win->RenderImage();
Fl::repeat_timeout(UPDATE_RATE, MyWindow::RenderImage_CB, userdata);
}
void MyWindow::PlotPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b) {
pixbuf[y][x][0] = r;
pixbuf[y][x][1] = g;
pixbuf[y][x][2] = b;
}
void MyWindow::RenderImage() {
static unsigned char drawcount = 0;
for ( int x=0; x<XSIZE; x++ )
for ( int y=0; y<YSIZE; y++ )
PlotPixel(x, y, x+drawcount, y+drawcount, x+y+drawcount);
++drawcount;
redraw();
}
#include "std_lib_facilities_4.h"
#include "Window.h"
#include "Graph.h"
#include "GUI.h"
#include <FL/Fl_Image.H>
using namespace Graph_lib;
using namespace std;
struct Lines_window:Graph_lib::Window{
Lines_window(Point xy, int w, int h, const string& title);
Button button_1;
Button button_2;
static void cb_change_color(Address, Address);
static void cb_change_picture(Address, Address);
};
Lines_window::Lines_window(Point xy, int w, int h, const string& title) :
Window(xy, w, h, title),
button_1(Point(x_max()/2, y_max()/2), 200, 100, "Button 1", cb_change_color),
button_2(Point(x_max()/3, y_max()/3), 200, 100, "Button 2", cb_change_picture)
{
attach(button_1);
attach(button_2);
}
void Lines_window::cb_change_color(Address, Address pw)
{
}
void Lines_window::cb_change_picture(Address, Address pw)
{
}
int main()
try {
if(H112 != 201401L)error("Error: incorrect std_lib_facilities_4.h version ", H112);
using namespace Graph_lib;
Lines_window win(Point(100,100),600,400,"Buttons");
return gui_main();
return 0;
}
catch(exception& e) {
cerr << "exception: " << e.what() << '\n';
return 1;
}
catch (...) {
cerr << "Some exception\n";
return 2;
}
This is my button code. I am trying to make two buttons, one that changes color when you press it and another that puts an image on the button when you press it. I haven't made a callback yet because this won't compile. Errors are:
GUI.cpp:16:6: error: prototype for ‘void Graph_lib::Button::attach(Graph_lib::Window&, Fl_Color)’ does not match any in class ‘Graph_lib::Button’
void Button::attach(Window& win, Fl_Color color)
^
In file included from GUI.cpp:10:0:
GUI.h:66:14: error: candidate is: virtual void Graph_lib::Button::attach(Graph_lib::Window&)
void attach(Window&);
^
Do I need callbacks to compile? I am basing this code off of my professor's code here. I took everything out except for the buttons so I could make them. The GUI.cpp and GUI.h were given to us. What am I doing wrong?
GUI.cpp
//
// This is a GUI support code to the chapters 12-16 of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
#include <FL/Fl.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Output.H>
#include "GUI.h"
namespace Graph_lib {
//------------------------------------------------------------------------------
void Button::attach(Window& win, Fl_Color color)
{
pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str());
pw->color(BLUE);
pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
own = &win;
}
//------------------------------------------------------------------------------
int In_box::get_int()
{
Fl_Input& pi = reference_to<Fl_Input>(pw);
// return atoi(pi.value());
const char* p = pi.value();
if (!isdigit(p[0])) return -999999;
return atoi(p);
}
//------------------------------------------------------------------------------
string In_box::get_string()
{
Fl_Input& pi = reference_to<Fl_Input>(pw);
return string(pi.value());
}
//------------------------------------------------------------------------------
void In_box::attach(Window& win)
{
pw = new Fl_Input(loc.x, loc.y, width, height, label.c_str());
own = &win;
}
//------------------------------------------------------------------------------
void Out_box::put(const string& s)
{
reference_to<Fl_Output>(pw).value(s.c_str());
}
//------------------------------------------------------------------------------
void Out_box::attach(Window& win)
{
pw = new Fl_Output(loc.x, loc.y, width, height, label.c_str());
own = &win;
}
//------------------------------------------------------------------------------
int Menu::attach(Button& b)
{
b.width = width;
b.height = height;
switch(k) {
case horizontal:
b.loc = Point(loc.x+offset,loc.y);
offset+=b.width;
break;
case vertical:
b.loc = Point(loc.x,loc.y+offset);
offset+=b.height;
break;
}
selection.push_back(b); // b is NOT OWNED: pass by reference
return int(selection.size()-1);
}
//------------------------------------------------------------------------------
int Menu::attach(Button* p)
{
Button& b = *p;
b.width = width;
b.height = height;
switch(k) {
case horizontal:
b.loc = Point(loc.x+offset,loc.y);
offset+=b.width;
break;
case vertical:
b.loc = Point(loc.x,loc.y+offset);
offset+=b.height;
break;
}
selection.push_back(&b); // b is OWNED: pass by pointer
return int(selection.size()-1);
}
//------------------------------------------------------------------------------
} // of namespace Graph_lib
GUI.h
//
// This is a GUI support code to the chapters 12-16 of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
#ifndef GUI_GUARD
#define GUI_GUARD
#include "Window.h"
#include "Graph.h"
namespace Graph_lib {
//------------------------------------------------------------------------------
typedef void* Address; // Address is a synonym for void*
typedef void(*Callback)(Address, Address); // FLTK's required function type for all callbacks
//------------------------------------------------------------------------------
template<class W> W& reference_to(Address pw)
// treat an address as a reference to a W
{
return *static_cast<W*>(pw);
}
//------------------------------------------------------------------------------
class Widget {
// Widget is a handle to an Fl_widget - it is *not* an Fl_widget
// We try to keep our interface classes at arm's length from FLTK
public:
Widget(Point xy, int w, int h, const string& s, Callback cb)
: loc(xy), width(w), height(h), label(s), do_it(cb)
{}
virtual void move(int dx,int dy) { hide(); pw->position(loc.x+=dx, loc.y+=dy); show(); }
virtual void hide() { pw->hide(); }
virtual void show() { pw->show(); }
virtual void attach(Window&) = 0;
Point loc;
int width;
int height;
string label;
Callback do_it;
virtual ~Widget() { }
protected:
Window* own; // every Widget belongs to a Window
Fl_Widget* pw; // connection to the FLTK Widget
private:
Widget& operator=(const Widget&); // don't copy Widgets
Widget(const Widget&);
};
//------------------------------------------------------------------------------
struct Button : Widget {
Button(Point xy, int w, int h, const string& label, Callback cb)
: Widget(xy,w,h,label,cb)
{}
void attach(Window&);
};
//------------------------------------------------------------------------------
struct In_box : Widget {
In_box(Point xy, int w, int h, const string& s)
:Widget(xy,w,h,s,0) { }
int get_int();
string get_string();
void attach(Window& win);
};
//------------------------------------------------------------------------------
struct Out_box : Widget {
Out_box(Point xy, int w, int h, const string& s)
:Widget(xy,w,h,s,0) { }
void put(int);
void put(const string&);
void attach(Window& win);
};
//------------------------------------------------------------------------------
struct Menu : Widget {
enum Kind { horizontal, vertical };
Menu(Point xy, int w, int h, Kind kk, const string& label)
: Widget(xy,w,h,label,0), k(kk), offset(0)
{}
Vector_ref<Button> selection;
Kind k;
int offset;
int attach(Button& b); // Menu does not delete &b
int attach(Button* p); // Menu deletes p
void show() // show all buttons
{
for (unsigned int i = 0; i<selection.size(); ++i)
selection[i].show();
}
void hide() // hide all buttons
{
for (unsigned int i = 0; i<selection.size(); ++i)
selection[i].hide();
}
void move(int dx, int dy) // move all buttons
{
for (unsigned int i = 0; i<selection.size(); ++i)
selection[i].move(dx,dy);
}
void attach(Window& win) // attach all buttons
{
for (int i=0; i<selection.size(); ++i) win.attach(selection[i]);
own = &win;
}
};
//------------------------------------------------------------------------------
} // of namespace Graph_lib
#endif // GUI_GUARD
I found the answer. It wouldn't compile because I added an argument to be passed to button (the Fl_Color). I will have to figure out a way to pass a value to the button to change the color.
EDIT: ANSWERED. Upon examination of the g.drawLines in my drawBackground function, I was drawing a point, not a line. It was user error.
Our school has written a header that simplifies much of allegro 5. I'm writing a program that will use that header (GameEngineV4) to simply emulate checkers, however, I'm having some difficulty getting the board to display to the screen.
Here is my code:
#include "GameEngineV4.h"
#include <iostream>
using namespace std;
void drawBackground(GameEngineV4, const int, const int);
int main()
{
const int BOARD_WIDTH = 600;
const int BOARD_MARGIN = 100;
GameEngineV4 g; // instantiate graphic object
g.initializePanel(800,800); // initialize Window
g.setBackground(g.CYAN); // color the background
drawBackground(g, BOARD_MARGIN, BOARD_WIDTH);
//* cleanup
g.displayPanel();
system("Pause");
g.displayDelay(1.0); // wait 10 seconds
g.closePanel();
return 0;
}
void drawBackground(GameEngineV4 g, const int BOARD_MARGIN, const int BOARD_WIDTH){
g.setColor(g.BLACK);
int i = 0;
int offset = (BOARD_WIDTH / 8) * i; //Variable to hold the amount a line should be shifted from original.
for (i = 0; i < 9; i++){
offset = (BOARD_WIDTH / 8) * i;
g.drawLine(BOARD_MARGIN, BOARD_MARGIN + offset, BOARD_MARGIN, BOARD_MARGIN + offset); //Horizontal lines
g.drawLine(BOARD_MARGIN + offset, BOARD_MARGIN, BOARD_MARGIN + offset, BOARD_MARGIN); //Vertical lines
}
}
The difficulty lies somewhere in the drawBackground function. I had tested the drawBackground function first by replacing the drawLine code in the for loop with g.drawLine(50, 50 + offset, 450, 450 + offset); and the code displayed just fine then. Why would changing it to calculate using constants affect my code?
Coded and compiled in Visual Studio 2013 on Windows 7.
EDIT: Of course, it would be a bit simpler if I included the header and implementation for GameEngineV4
Here's GameEngineV4.h:
#ifndef _GameEngineV4
#define _GameEngineV4
#include <string>
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h> //Our primitive header file
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
using namespace std;
class GameEngineV4
{
private:
int gSleep;
int gSpeed;
bool appRunning;
ALLEGRO_COLOR colortable[32];
ALLEGRO_FONT * fonttable[32];
int keyTable[256];
int default_pen;
int default_brush;
int default_font;
int default_BGColor;
double default_thickness;
bool keyPressed;
int pressedChar;
ALLEGRO_DISPLAY *gdisplay;
public:
GameEngineV4();
//-------------------------------------------------
// Game Engine Functions
//-------------------------------------------------
// call to return last key pressed
int gameLastkey();
// call to set game speed;
void gameSetGameSpeed(int);
void gameSetSpeed(int);
// call to set game speed;
int gameGetGameSpeed();
int gameGetSpeed();
// call to set display delay;
void gameSetDisplayDelay(int);
// start game
int gameEngineMain(void);
// call to repaint window
void gameRepaint();
// creates and fills a rectangle color is index into standard color table
void fill_rect(int xl,int yl,int xr,int yr,int penColor,int brushColor);
void fill_rect(int x1,int y1,int w,int h);
// creates and fills an ellipse
void fill_ellipse(int x1 ,int y ,int w,int h,int penColor,int brushColor);
void fill_ellipse(int x1,int y1,int w,int h);
// text out string, x, y, color code
void text_out(char *,int ,int ,int);
void text_out(char *txt, int x,int y);
void text_out(LPCSTR txt,int x,int y,int clr);
void text_out(LPCSTR txt,int x,int y);
// draw line from point1(x1,y1) to point2(x2,y2)
void draw_line(int x1,int y1,int x2,int y2); // uses default color
void draw_line(int x1,int y1,int x2,int y2,int color);
// draws ellipse
void draw_ellipse(int x1,int y1,int w,int h,int penColor,int brushColor);
void draw_ellipse(int x1,int y1,int w,int h); // uses default color
// draws rectangle
void draw_rect(int x1,int y1,int w,int h,int penColor,int brushColor);
void draw_rect(int x1,int y1,int w,int h);
// sets default color
void color(int pen,int brush);
// sets default pen color
void pen_color(int pen);
// sets default brush
void brush_color(int brush);
// register call backs
void registerGameMain(void (*fun)());
void registerGameInit(void (*fun)());
void registerGameQuit(void (*fun)());
void registerGamePaint(void (*fun)());
void registerGameKeyUp(void (*fun)(int));
void registerGameKeyDown(void (*fun)(int));
void registerGameLMouse(void (*fun)(int,int,int));
void registerGameRMouse(void (*fun)(int,int,int));
void registerGameMoveMouse(void (*fun)(int,int,int));
void playBeep1();
void playBeep2();
void playBeep(float g, float s );
void playWav(char * filename, float gain, float speed, float duration);
void playWave(string filename, float gain, float speed, float duration);
int initializeAudio(int samples);
//-----------------------------------------------
// call back functions
//-----------------------------------------------
// called during initialization
void gameInit();
// called during initialization
void gameMain();
// called at exit
void gameQuit();
// called to repaint screen
void gamePaint();
// keyboard and mouse functions
void gameKeyUp(int);
void gameKeyDown(int);
void gameLMouse(int,int,int);
void gameRMouse(int,int,int);
void gameMoveMouse(int,int,int);
// initialization methods
void initColorTable();
void initFontTable();
void initKeyTable();
void GoPaint();
// Alternate Graphics functions
int initializePanel(int width, int height);
int initializePanel(int width, int height, int color);
void closePanel();
void displayPanel();
void displayDelay(double delay);
void setBackground(int color);
void setBackground(int r, int g, int b);
void setThickness(double t);
void setPenColor(int pen);
void setColor(int pen);
void setColor(int r, int g, int b);
void setBrushColor(int brush);
void setBrushColor(int r, int g, int b);
void setFont(int font);
void setFontSize(int fontSize, int flag);
void setFont(string family, int fontSize, int flag);
void drawLine(int x1, int y1, int x2, int y2);
void drawOval(int x1,int y1,int w,int h);
void drawRect(int x1,int y1,int w,int h);
void drawString(char *txt,int x,int y);
void drawString(string txt,int x,int y);
void drawARC(int x1,int y1,int r, int w,int h,float st, float dt);
void fillOval(int x1,int y1,int w,int h);
void fillRect(int x1,int y1,int w,int h);
void drawTriangle(float x1, float y1, float x2, float y2 , float x3, float y3);
void fillTriangle(float x1, float y1, float x2, float y2 , float x3, float y3);
void fillRibbon(int x1,int y1,int x2,int y2, int w);
void drawRibbon(int x1,int y1,int x2,int y2, int w);
enum color{BLACK,BLUE,CYAN,DARK_GRAY,GRAY,GREEN,
LIGHT_GRAY,MAGENTA,ORANGE,PINK,RED,WHITE,YELLOW,
MAROON,PURPLE,LIME,OLIVE,NAVY,TEAL};
};
#endif
And here's the implementation, GameEngineV4.cpp:
//-------------------------------------------------------
//-------------------------------------------------------
// GameEngine Version 4.2 - Allegro, class Based, and
// registered callbacks
// 4.1
// 4.2 Enhanced Audio
//-------------------------------------------------------
//-------------------------------------------------------
#include <iostream>
#include <string>
#include "GameEngineV4.h"
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h> //Our primitive header file
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
using namespace std;
//-------------------------------------------------------
// Definitions
//-------------------------------------------------------
#define WINDOW_WIDTH 800 // default size of game window
#define WINDOW_HEIGHT 600
#define T2SLEEP 20 // thread 2 default sleep time
#define T1SPEED 700000 // thread 1 default delay loops
//-------------------------------------------------------
// Global Variables
//-------------------------------------------------------
ALLEGRO_SAMPLE *beep1 = NULL;
ALLEGRO_SAMPLE *beep2 = NULL;
//-------------------------------------------------------
// Function pointers
//-------------------------------------------------------
void (*pGameMain) ()=NULL;
void (*pGameInit) ()=NULL;
void (*pGameQuit) ()=NULL;
void (*pGamePaint) ()=NULL;
void (*pGameKeyUp) (int)=NULL;
void (*pGameKeyDown) (int)=NULL;
void (*pGameLMouse) (int,int,int)=NULL;
void (*pGameRMouse) (int,int,int)=NULL;
void (*pGameMoveMouse) (int,int,int)=NULL;
enum KEYS{ UP, DOWN, LEFT, RIGHT};
GameEngineV4::GameEngineV4()
{
gSleep=T2SLEEP;
gSpeed=T1SPEED;
appRunning=false;
for (int i=0;i<256;i++)
keyTable[i]=0;
default_pen=0;
default_brush=0;
default_font=0;
default_BGColor=0;
default_thickness=2.0;
gdisplay = NULL;
}
int GameEngineV4::gameEngineMain(void)
{
int width = WINDOW_WIDTH;
int height = WINDOW_HEIGHT;
bool done = false;
bool redraw = true;
int pos_x = width / 2;
int pos_y = height / 2;
int FPS = 60;
bool keys[4] = {false, false, false, false};
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
if(!al_init()) //initialize Allegro
return -1;
display = al_create_display(width, height); //create our display object
if(!display) //test display object
return -1;
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(10);
beep1 = al_load_sample("beep1.ogg");
beep2 = al_load_sample("beep2.ogg");
initColorTable();
initFontTable();
initKeyTable();
gameInit();
//CreateThread(0,0,ThreadProc1,0,0,0);
//CreateThread(0,0,ThreadProc2,0,0,0);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / gameGetSpeed());
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
keyPressed=true;
pressedChar=keyTable[(int)ev.keyboard.keycode];
gameKeyDown(keyTable[(int)ev.keyboard.keycode]);
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
keyPressed=true;
pressedChar=keyTable[(int)ev.keyboard.keycode];
gameKeyUp(keyTable[(int)ev.keyboard.keycode]);
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
gameMain();
redraw = true;
}else if (ev.type== ALLEGRO_EVENT_MOUSE_AXES)
{
gameMoveMouse(ev.mouse.x,ev.mouse.y,1);
}else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if (ev.mouse.button & 1)
{
gameLMouse(ev.mouse.x,ev.mouse.y,1);
}
if (ev.mouse.button & 2)
{
gameRMouse(ev.mouse.x,ev.mouse.y,1);
}
}
if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
gamePaint();
al_flip_display();
al_clear_to_color(colortable[default_BGColor]);
}
}
al_destroy_sample(beep1);
al_destroy_sample(beep2);
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
al_destroy_display(display); //destroy our display object
return 0;
}
/*
//-------------------------------------------------------
// Additional Game Threads
//-------------------------------------------------------
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
gameMain1();
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
while(appRunning)
{
gameMain2();
GoPaint(NULL,NULL);
Sleep(gSleep);
}
return 0;
}
*/
//-------------------------------------------------------
// register callbacks and callbacks
//-------------------------------------------------------
void GameEngineV4:: registerGameMain(void (*fun)() )
{
pGameMain = fun ;
}
void GameEngineV4::gameMain()
{
if (pGameMain !=NULL) pGameMain();
}
void GameEngineV4:: registerGameInit(void (*fun)() )
{
pGameInit = fun ;
}
void GameEngineV4::gameInit()
{
if (pGameInit !=NULL) pGameInit();
}
void GameEngineV4:: registerGameQuit(void (*fun)() )
{
pGameQuit = fun ;
}
void GameEngineV4::gameQuit()
{
if (pGameQuit !=NULL) pGameQuit();
}
void GameEngineV4:: registerGamePaint(void (*fun)() )
{
pGamePaint = fun ;
}
void GameEngineV4::gamePaint()
{
if (pGamePaint !=NULL) pGamePaint();
}
void GameEngineV4:: registerGameKeyUp(void (*fun)(int) )
{
pGameKeyUp = fun ;
}
void GameEngineV4::gameKeyUp(int x)
{
if (pGameKeyUp !=NULL) pGameKeyUp(x);
}
void GameEngineV4:: registerGameKeyDown(void (*fun)(int) )
{
pGameKeyDown = fun ;
}
void GameEngineV4::gameKeyDown(int x)
{
if (pGameKeyDown !=NULL) pGameKeyDown(x);
}
void GameEngineV4:: registerGameLMouse(void (*fun)(int,int,int) )
{
pGameLMouse = fun ;
}
void GameEngineV4::gameLMouse(int x, int y, int z)
{
if (pGameLMouse !=NULL) pGameLMouse(x, y, z);
}
void GameEngineV4:: registerGameRMouse(void (*fun)(int,int,int) )
{
pGameRMouse = fun ;
}
void GameEngineV4::gameRMouse(int x, int y, int z)
{
if (pGameRMouse !=NULL) pGameRMouse(x, y, z);
}
void GameEngineV4:: registerGameMoveMouse(void (*fun)(int,int,int) )
{
pGameMoveMouse = fun ;
}
void GameEngineV4::gameMoveMouse(int x, int y, int z)
{
if (pGameMoveMouse !=NULL) pGameMoveMouse(x, y, z);
}
//-------------------------------------------------------
// Pens and Brushes
//-------------------------------------------------------
void GameEngineV4::initColorTable()
{
colortable[0]=al_map_rgb(0,0,0); // black
colortable[1]=al_map_rgb(0,0,255); // blue
colortable[2]=al_map_rgb(0,255,255); // cyan /aqua
colortable[3]=al_map_rgb(96,96,96); // dark gray
colortable[4]=al_map_rgb(160,160,160); // gray
colortable[5]=al_map_rgb(0,160,0); // green
colortable[6]=al_map_rgb(192,192,192); // light gray /silver
colortable[7]=al_map_rgb(255,0,255); // magenta
colortable[8]=al_map_rgb(255,165,0); // orange
colortable[9]=al_map_rgb(255,160,203); // pink
colortable[10]=al_map_rgb(255,0,0); // red
colortable[11]=al_map_rgb(255,255,255); // white
colortable[12]=al_map_rgb(255,255,0); // yellow
colortable[13]=al_map_rgb(160,0,0); // maroon
colortable[14]=al_map_rgb(160,0,160); // purplE
colortable[15]=al_map_rgb(0,255,0); // lime
colortable[16]=al_map_rgb(160,160,0); // olive
colortable[17]=al_map_rgb(0,0,160); // navy
colortable[18]=al_map_rgb(0,160,160); // teal
}
void GameEngineV4::initFontTable()
{
fonttable[0] = al_load_font("arial.ttf", 16, 1);
fonttable[1] = al_load_font("arial.ttf", 32, 2);
fonttable[2] = al_load_font("arial.ttf", 48, 2);
}
void GameEngineV4::initKeyTable()
{
for (int i=0; i< 256;i++)
keyTable[i]=0; // zero
keyTable[84]=0x26; // keyup
keyTable[85]=0x28; // key down
keyTable[82]=0x25; // left
keyTable[83]=0x27; // right
for (int i=0; i< 26;i++)
keyTable[i+1]=i+0x41; // a-z
for (int i=0; i< 9;i++)
keyTable[i+27]=i+0x30; // 0-9
for (int i=0; i< 9;i++)
keyTable[i+37]=i+0x60; // 0-9 key pad
for (int i=0; i< 12;i++)
keyTable[i+47]=i+0x70; // F1-F12
keyTable[59]=0x1b; // escape
keyTable[61]=0x6d; // minus
keyTable[63]=0x08; // backspace
keyTable[64]=0x09; // tab
keyTable[75]=0x20; // space
keyTable[67]=0x0d; // return or enter
}
//-------------------------------------------------------
// Text Procssing
//-------------------------------------------------------
void GameEngineV4::text_out(char *txt,int x,int y,int clr)
{
al_draw_text(fonttable[default_font], colortable[clr], x, y, 0, txt);
}
void GameEngineV4::text_out(char *txt,int x,int y)
{
al_draw_text(fonttable[default_font], colortable[default_pen], x, y, 0, txt);
}
//-------------------------------------------------------
// Functions to Draw Lines and Shapes
//-------------------------------------------------------
void GameEngineV4::draw_line(int x1,int y1,int x2,int y2)
{
al_draw_line(x1, y1, x2, y2, colortable[default_pen],default_thickness);
}
void GameEngineV4::draw_line(int x1,int y1,int x2,int y2,int clr)
{
al_draw_line(x1, y1, x2, y2, colortable[clr] ,default_thickness);
}
void GameEngineV4::draw_ellipse(int x1,int y1,int w,int h,int penClr,int brushClr)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1+w1, y1+h1, w1, h1, colortable[penClr], default_thickness);
}
void GameEngineV4::draw_rect(int x1,int y1,int w,int h)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[default_pen],default_thickness);
}
void GameEngineV4::draw_rect(int x1,int y1,int w,int h,int penClr,int brushClr)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[penClr],default_thickness);
}
void GameEngineV4::draw_ellipse(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1, y1, w1, h1, colortable[default_pen], default_thickness);
}
void GameEngineV4::fill_ellipse(int x1,int y1,int w,int h,int penClr,int brushClr)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[penClr]);
}
void GameEngineV4::fill_ellipse(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[default_brush]);
}
void GameEngineV4::fill_rect(int x1,int y1,int w,int h)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[default_brush]);
}
void GameEngineV4::fill_rect(int x1,int y1,int w,int h,int penClr,int brushClr)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[penClr]);
}
void GameEngineV4::color(int pen,int brush)
{
default_pen=pen;
default_brush=brush;
}
void GameEngineV4::pen_color(int pen)
{
default_pen=pen;
}
void GameEngineV4::brush_color(int brush)
{
default_brush=brush;
}
int GameEngineV4::gameLastkey()
{
if(!keyPressed)
{
return 0;
}
else
{
keyPressed=false;
return pressedChar;
}
}
void GameEngineV4::gameSetSpeed(int x)
{
gSpeed=x;
}
void GameEngineV4::gameSetGameSpeed(int x)
{
gSpeed=x;
}
void GameEngineV4::gameSetDisplayDelay(int x)
{
gSleep=x;
}
int GameEngineV4::gameGetSpeed()
{
return gSpeed;
}
int GameEngineV4::gameGetGameSpeed()
{
return gSpeed;
}
// graphics interface
int GameEngineV4::initializePanel(int width, int height)
{
if(!al_init()) //initialize Allegro
return -1;
gdisplay = al_create_display(width, height); //create our display object
if(!gdisplay) //test display object
return -1;
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(100);
initColorTable();
initFontTable();
initKeyTable();
return 0;
}
void GameEngineV4::closePanel()
{
al_destroy_display(gdisplay); //destroy our display object
return;
}
void GameEngineV4::displayPanel()
{
al_flip_display();
}
void GameEngineV4::setBackground(int color)
{
default_BGColor =color;
al_clear_to_color(colortable[default_BGColor]);
}
void GameEngineV4::setBackground(int r, int g, int b)
{
colortable[30] =al_map_rgb(r,g,b);
default_BGColor =30;
al_clear_to_color(colortable[default_BGColor]);
}
void GameEngineV4::setThickness(double t)
{
default_thickness=t;
}
void GameEngineV4::setPenColor(int pen)
{
default_pen=pen;
}
void GameEngineV4::setColor(int clr)
{
default_pen=clr;
default_brush=clr;
}
void GameEngineV4::setColor(int r, int g, int b)
{
colortable[31] =al_map_rgb(r,g,b);
default_pen=31;
}
void GameEngineV4::setBrushColor(int brush)
{
default_brush=brush;
}
void GameEngineV4::setBrushColor(int r, int g, int b)
{
colortable[30] =al_map_rgb(r,g,b);
default_brush=30;
}
void GameEngineV4::setFont(int font)
{
default_font=font;
}
void GameEngineV4::setFontSize(int fontSize, int flag)
{
fonttable[31] = al_load_font("arial.ttf", fontSize, flag);
default_font=31;
}
void GameEngineV4::setFont(string family, int fontSize, int flag)
{
fonttable[31] = al_load_font(family.c_str(), fontSize, flag);
default_font=31;
}
void GameEngineV4::drawLine(int x1, int y1, int x2, int y2)
{
al_draw_line(x1, y1, x2, y2, colortable[default_pen] ,default_thickness);
}
void GameEngineV4::drawOval(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_ellipse(x1+w1, y1+h1, w1, h1, colortable[default_pen], default_thickness);
}
void GameEngineV4::drawRect(int x1,int y1,int w,int h)
{
al_draw_rectangle(x1,y1,x1+w,y1+h,colortable[default_pen],default_thickness);
}
void GameEngineV4::drawString(char *txt,int x,int y)
{
text_out(txt,x,y);
}
void GameEngineV4::drawString(string txt,int x,int y)
{
al_draw_text(fonttable[default_font], colortable[default_pen], x, y, 0, txt.c_str());
}
void GameEngineV4::fillOval(int x1,int y1,int w,int h)
{
int w1=w/2;
int h1=h/2;
al_draw_filled_ellipse (x1+w1,y1+h1,w1,h1,colortable[default_brush]);
}
void GameEngineV4::fillRect(int x1,int y1,int w,int h)
{
al_draw_filled_rectangle(x1,y1,x1+w,y1+h,colortable[default_brush]);
}
void GameEngineV4::displayDelay(double d)
{
al_rest(d);
}
void GameEngineV4::drawARC(int x1,int y1, int r, int w,int h,float st, float dt)
{
int w1=w/2;
int h1=h/2;
float dt1=3.14 * (dt*-1)/180.0;
float st1=3.14 * (st*-1)/180.0;
al_draw_arc(x1+w1,y1+h1,r,st1,dt1,colortable[default_brush],default_thickness);
}
void GameEngineV4::playBeep1()
{
al_play_sample(beep1, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playBeep2()
{
al_play_sample(beep2, 1, 0, 1 , ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playBeep(float g, float s )
{
al_play_sample(beep1, g, 0, s, ALLEGRO_PLAYMODE_ONCE, 0);
}
void GameEngineV4::playWav(char filename[], float gain, float speed, float duration )
{
ALLEGRO_SAMPLE *soundeffect = al_load_sample(filename);
al_play_sample(soundeffect,gain,0.0, speed, ALLEGRO_PLAYMODE_ONCE, 0);
al_rest(duration);
al_destroy_sample(soundeffect);
}
void GameEngineV4::playWave(string filename, float gain, float speed, float duration )
{
ALLEGRO_SAMPLE *soundeffect = al_load_sample(filename.c_str());
al_play_sample(soundeffect,gain,0.0, speed, ALLEGRO_PLAYMODE_ONCE, 0);
al_rest(duration);
al_destroy_sample(soundeffect);
}
// audio interface
int GameEngineV4::initializeAudio(int samples)
{
if(!al_init()) //initialize Allegro
return -1;
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(samples);
return 0;
}
void GameEngineV4::drawTriangle(float x1, float y1, float x2, float y2 , float x3, float y3)
{
al_draw_triangle(x1,y1,x2,y2,x3,y3,colortable[default_pen],default_thickness);
}
void GameEngineV4::fillTriangle(float x1, float y1, float x2, float y2 , float x3, float y3)
{
al_draw_filled_triangle(x1,y1,x2,y2,x3,y3,colortable[default_brush]);
}
void GameEngineV4::fillRibbon(int x1,int y1,int x2,int y2, int w)
{
struct MyPoint
{
float x, y;
};
const size_t num_points1 = 2;
MyPoint points1[] =
{
{x1, y1},
{x2, y2}
};
al_draw_ribbon((float*)points1, sizeof(MyPoint), colortable[default_brush], w, num_points1);
}
void GameEngineV4::drawRibbon(int x1,int y1,int x2,int y2, int w)
{
fillRibbon(x1,y1,x2,y2, w);
}
EDIT: ANSWERED. See top for more info.
In my drawBackground function, I am supposed to provide the starting point of the line and the ending point of the line. I provided the same point for both the start and end point. End result, nothing displayed.