To solve the problem of Vesa or Vga or OpenGL other other driver, just use the abstract factory pattern. If you implement this from the beginning then your gui code does not need to change only add a driver and voila works.
Code: Select all
#include <stdio.h>
#include <string.h>
typedef class Line {
protected: int _x1, _y1, _x2, _y2;
public: Line(int x1, int y1, int x2, int y2) : _x1(x1), _y1(y1), _x2(x2), _y2(y2) {}
public: virtual void draw(void) = 0;
} Line;
typedef class DrawDibLine : public Line {
public: DrawDibLine(int x1, int y1, int x2, int y2) : Line(x1, y1, x2, y2) {}
public: virtual void draw(void) {
printf("DrawDib::Line(%d, %d)-(%d, %d)\n", _x1, _y1, _x2, _y2);
}
} DrawDibLine;
typedef class OpenGLLine : public Line {
public: OpenGLLine(int x1, int y1, int x2, int y2) : Line(x1, y1, x2, y2) {}
public: virtual void draw(void) {
printf("OpenGL::Line(%d, %d)-(%d, %d)\n", _x1, _y1, _x2, _y2);
}
} OpenGLLine;
typedef class VesaLine : public Line {
public: VesaLine(int x1, int y1, int x2, int y2) : Line(x1, y1, x2, y2) {}
public: virtual void draw(void) {
printf("Vesa::Line(%d, %d)-(%d, %d)\n", _x1, _y1, _x2, _y2);
}
} VesaLine;
typedef class GUIFactory {
public: static GUIFactory * GetInstance(char *);
public: virtual Line * CreateLine(int x1, int y1, int x2, int y2) = 0;
} GUIFactory;
typedef class GUIDrawDibFactory : public GUIFactory {
public: virtual Line * CreateLine(int x1, int y1, int x2, int y2) {
return(new DrawDibLine(x1, y1, x2, y2));
}
} GUIDrawDibFactory;
typedef class GUIOpenGLFactory : public GUIFactory {
public: virtual Line * CreateLine(int x1, int y1, int x2, int y2) {
return(new OpenGLLine(x1, y1, x2, y2));
}
} GUIOpenGLFactory;
typedef class GUIVesaFactory : public GUIFactory {
public: virtual Line * CreateLine(int x1, int y1, int x2, int y2) {
return(new VesaLine(x1, y1, x2, y2));
}
} GUIVesaFactory;
GUIFactory * GUIFactory::GetInstance(char * type) {
if(type != 0) {
if(strcmp(type, "DrawDib") == 0) {
return(new GUIDrawDibFactory);
}
else
if(strcmp(type, "OpenGL") == 0) {
return(new GUIOpenGLFactory);
}
else
if(strcmp(type, "Vesa") == 0) {
return(new GUIVesaFactory);
}
}
return(new GUIDrawDibFactory);
}
int main(int argc, char * argv[]) {
GUIFactory * guiFactory = GUIFactory::GetInstance(argv[1]);
/* actual gui calls for instance, button, window, etc.
Line * line = guiFactory->CreateLine(10, 10, 246, 246);
line->draw();
return(0);
}
when you compile this you get and application say gui.exe and you can switch between modes with the first argument being either DrawDib, OpenGL or Vesa (case sensitive). The main function will not change however the behaivour does.