I'm creating (for learning purposes) a 2D game in embarcadero's Firemonkey XE2 platform. It's a game similar to space invanders in terms of visual representation of the game field but vastly more complex.
I'm trying to spawn animated enemies. I created a template TImage and assigned it a BitmapListAnimation with six pictures. Then i just clone it using Clone(). Here is the cloning code:
virtual void CreateMe(TForm* pForm);
void CBaseEnemy::CreateMe(TForm *pForm)
{
TImage* pSource = dynamic_cast<TImage*>(this->pToClone);
pSource->Visible = true;
TImage* pDest = dynamic_cast<TImage*>(pSource->Clone(pForm));
pDest->Parent = pForm;
TBitmapListAnimation* pAnimSource = dynamic_cast<TBitmapListAnimation*>(pForm->FindComponent("BitmapListAnimation1"));
pAnimSource->Parent = pDest;
pAnimSource->Start();
TBitmapListAnimation* pAnimDest = dynamic_cast<TBitmapListAnimation*>(pAnimSource->Clone(pForm));
}
And it works, It's just extremely complicated, could you suggest a way to clone an entire template (A Component such as TImage along with all it's children) ?
An Embarcadero blog entry just released covers this: blog
Related
Banging my head on the wall trying to organize what I feel should be (and probably is) a simple set of relationships between some classes.
Basically trying to tie together 3 classes together in a way that makes sense.
Simplified scenario with 3 classes:
1 - LCD device driver
2 - Simple graphics library
3 - Counter display class
What I've got so far in pseudocode:
class Driver : public Graphics
{
public:
void loadImage(int * image){
// load image into device memory
}
};
class Graphics
{
public:
int image[10];
void displayImage(int * image){
// create/ manipulate image here and...
loadImage(image); //send to device
}
virtual void loadImage(int * image){}
};
class Counter
{
public:
int counterImage[10];
void makeCounter(int * counterImage){
//make a clock counter graphic and…
displayImage(counterImage);
}
};
Obviously, I've not figured out how to get the displayImage(counterImage) function integrated into the Counter class. I could virtual a version of displayImage() in the Counter class, but I'm assuming that that would entail that Graphics would always have to inherit Counter, which I'm not keen on. Is there a better way to allow Counter to access the Graphics class functions (ultimately passing through to the LCD driver) while still remaining separate from it?
Why do you want to use inheritance at all?
Based on the description on your classes, I don't see any specialization / kind of relation between them, which means you you should use composition in this case:
the graphics Driver needs the ability to display some Image (a type not present in your example)
the Graphics image loading library needs the ability to load an Image
the Counter display should use both a Driver and a Graphics, both given to it in its constructor, displaying the counter with them.
This concept is called composition over inheritance, you can get a lot more good articles on it with google. (Basically: OOP and using classes doesn't mean you have to use inheritance for everything)
I am porting an app to Qt and have troubles with the integration of the Syphon framework (http://syphon.v002.info/) which is used for video streams sharing between applications via the GPU (Mac OS X only).
Two C++ implementations for Syphon are available, one for Cinder (github/astellato/Cinder-Syphon) and one for openFrameworks (github/astellato/ofxSyphon). I started from the Cinder implementation (both are quite similar) and tried to port it to Qt but I can't find a way to create a QOpenGlTexture using an already created texture.
Here is the code from Cinder-Syphon that I'm trying to get to work (file syphonClient.mm) :
void syphonClient::bind()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if(bSetup)
{
[(SyphonNameboundClient*)mClient lockClient];
SyphonClient *client = [(SyphonNameboundClient*)mClient client];
latestImage = [client newFrameImageForContext:CGLGetCurrentContext()];
NSSize texSize = [(SyphonImage*)latestImage textureSize];
GLuint m_id = [(SyphonImage*)latestImage textureName];
mTex = ci::gl::Texture::create(GL_TEXTURE_RECTANGLE_ARB, m_id,
texSize.width, texSize.height, true);
mTex->setFlipped();
mTex->bind();
}
else
std::cout<<"syphonClient is not setup, or is not properly connected to server. Cannot bind.\n";
[pool drain];
}
In the Qt version I'm writing, mTex is of type QOpenGlTexture but I couldn't find an equivalent of mTex = ci::gl::Texture::create(GL_TEXTURE_RECTANGLE_ARB, m_id, texSize.width, texSize.height, true);, that is, creating a QOpenGLTexture using an existing texture ID without having to allocate storage again.
Did I miss something in the Qt OpenGL API or is it just not possible ? If not I guess I will have to use direct OpenGL calls or pulling in the whole Cinder OpenGL API ?
I got a stupid question on ListView Control usage.
I created a Windows Form App in VS2005. No I dragged a ListView Control from the toolbox. I want to implement my code to show some content(including both columns and rows).
I know a little of MFC knowledge. I am not sure I must study the past MFC CListCtrol knowledge to implement my application or I can just study the System.Windows.Forms::ListView simply.
I found a good sample working with ListView (but wrote in C#). Can I translate the sample code from C# to C++ in VS2005? If I can. Could you please give me some suggestions?
using System;
using System.Windows.Forms;
using System.Drawing;
public class ListView1 : Form {
ListView listView = new ListView();
public ListView1() {
listView.Dock = DockStyle.Fill;
PopulateListView();
this.Controls.Add(listView);
this.ClientSize = new Size(400, 200);
}
private void PopulateListView() {
// Set the view to show details.
listView.View = View.Details;
// Add columns
listView.Columns.Add("Author",
-2,
HorizontalAlignment.Center);
listView.Columns.Add("Title",
-2,
HorizontalAlignment.Left);
listView.Columns.Add("Price",
-2,
HorizontalAlignment.Left);
// Add items
ListViewItem item1 = new ListViewItem("Steve Martin");
item1.SubItems.Add("Programming .NET");
item1.SubItems.Add("39.95");
ListViewItem item2 = new ListViewItem("Irene Suzuki");
item2.SubItems.Add("VB.NET Core Studies");
item2.SubItems.Add("69.95");
ListViewItem item3 = new ListViewItem("Ricky Ericsson");
item3.SubItems.Add("Passing Your .NET Exams");
item3.SubItems.Add("19.95");
// Add the items to the ListView.
listView.Items.AddRange(
new ListViewItem[] {item1,
item2,
item3}
);
}
public static void Main() {
ListView1 form = new ListView1();
Application.Run(form);
}
}
Actually you don't need that much of your previous knowledge of MFC to implement ListView. C++ under .NET (in layman terms means WinForm applications), you can almost seamlessly translate C# code to C++. If I understood your question correctly, what you need to do is to make sure how objects and properties are accessed in C++ if you are developing a winforms app. Like in C# if you have Object.function, in C++ you may need to write Object::function, this is just an example. Definitely you would need to some more in depth knowledge to run things seamlessly.
I was studying the Model-View-Controller design pattern and i understand the concept behind the pattern theorotically, but I wanted to get a peek at how one would actually put it to practice.
Wikipedia mentions Wt - Web toolkit, CppCMS and some other standard implementations which use the pattern however I have not been familiar with these, and I was just hoping and
will be really grateful If anyone can provide some sample code(hopefully C++) which implements the pattern and explains the theory of the pattern being put to practice.
Here's a quick example I made (didn't try compiling it, let me know if there's errors):
class Button; // Prewritten GUI element
class GraphGUI {
public:
GraphGUI() {
_button = new Button("Click Me");
_model = new GraphData();
_controller = new GraphController(_model, _button);
}
~GraphGUI() {
delete _button;
delete _model;
delete _controller;
}
drawGraph() {
// Use model's data to draw the graph somehow
}
...
private:
Button* _button;
GraphData* _model;
GraphController* _controller;
};
class GraphData {
public:
GraphData() {
_number = 10;
}
void increaseNumber() {
_number += 10;
}
const int getNumber() { return _number; }
private:
int _number;
};
class GraphController {
public:
GraphController(GraphData* model, Button* button) {
__model = model;
__button = button;
__button->setClickHandler(this, &onButtonClicked);
}
void onButtonClicked() {
__model->increaseNumber();
}
private:
// Don't handle memory
GraphData* __model;
Button* __button;
};
Ignoring the implementation of Button, basically this program will use GraphGUI to display a graph that will change when a button is pressed. Let's say it's a bar graph and it will get taller.
Since the model is independent of the view (the button), and the controller handles the communication between the two, this follows the MVC pattern.
When the button is clicked, the controller modifies the model via the onButtonClicked function, which the Button class knows to call when it is clicked.
The beauty of this is since the model and view are completely independent, the implementation of each can drastically change and it won't affect the other, the controller might simply have to make a few changes. If the model in this case calculated some result based off some database data, then clicking the button could cause this to happen, but the button implementation wouldn't have to change. Or, instead of telling the controller when a click occurs, maybe it can tell the controller when the button is moused-over. The same changes are applied to model, regardless of what triggered the changes.
A simple text editor could be designed based on MVC. Think of the string class as the model, where data is stored. We might have a class called SimpleTextView which displays the text in the string attached to it, as it is. A class called KeyboardEventHandler can act as the controller. The controller will notify the view about new keyboard events. The view in turn modifies the model (like appending or removing text). The changes in the model is reflected on all views attached to it. For instance, there might be another view called HtmlView attached to the string object manipulated from within the SimpleTextView. If the user enters valid HTML tags in the SimpleTextView, the HtmlView will display the formatted output - real-time.
There are couple of complete MVC examples, plus discussion, in ch 2 of an introduction to programming in Python 3.x that I wrote (I've not completed ch 3 etc., that project's been on ice for some time -- Python community really like angry swarm of bees when discovered I'd written that Python was perhaps not suitable for very large scale development, so it became difficult to get sensible feedback). It's available in PDF format from Google Docs. I don't know how well it maps to common MVC implementations, I was mostly concerned with getting the general idea across. :-)
Cheers & hth.,
PS: There's a nice table of contents in the PDF file but Google Docs doesn't show it. You'd need to dl and use Foxit or Acrobat or some other PDF viewer. I think there's a separate viewable TOC at Google Docs, though, haven't checked and don't remember whether updated.
PPS: Forgot to mention, the MVC image processing example near the end has nice pic of Lena Söderberg! :)
Code is the best approach to understand and learn Model View Controller:
Here is a simple JS example (from Wiki)
/** Model, View, Controller */
var M = {}, V = {}, C = {};
/** Model stores data */
M.data = "hello world";
/** View controls what to present */
V.render = (M) => { alert(M.data); }
/** Controller bridges View and Model */
C.handleOnload = () => { V.render(M); }
/** Controller on Windows OnLoad event */
window.onload = C.handleOnload;
Here is a detailed post in C/C++
Model-View-Controller Explained in C++
I've made many different seperate parts of a GUI system for the Nintendo DS, like buttons and textboxes and select boxes, but I need a way of containing these classes in one Gui class, so that I can draw everything to the screen all at once, and check all the buttons at once to check if any are being pressed. My question is what is the best way organize all the classes (such as buttons and textboxes) into one GUI class?
Here's one way I thought of but it doesn't seem right:
Edit: I'm using C++.
class Gui {
public:
void update_all();
void draw_all() const;
int add_button(Button *button); // Returns button id
void remove_button(int button_id);
private:
Button *buttons[10];
int num_buttons;
}
This code has a few problems, but I just wanted to give you an idea of what I want.
This question is very similar to one I was going to post, only mine is for Sony PSP programming.
I've been toying with something for a while, I've consulted some books and VTMs, and so far this is a rough idea of a simple ui systems.
class uiElement()
{
...
virtual void Update() = 0;
virtual void Draw() = 0;
...
}
class uiButton() public : uiElement
{
...
virtual void Update();
virtual void Draw();
...
}
class uiTextbox() public : uiElement
{
...
virtual void Update();
virtual void Draw();
...
}
... // Other ui Elements
class uiWindow()
{
...
void Update();
void Draw();
void AddElement(uiElement *Element);
void RemoveElement(uiElement *Element);
std::list <uiElement*> Elements;
...
}
void uiWindow::Update()
{
...
for (list <uiElement*>::iterator it = Elements.begin(); it != Elements.end(); it++ )
it->Update();
...
}
void uiWindow::Draw()
{
...
for (list <uiElement*>::iterator it = Elements.begin(); it != Elements.end(); it++ )
it->Draw();
...
}
The princple is to create a window and attact ui Elements to it, and call the draw and update methods from the respective main functions.
I don't have anything working yet, as I have issues with drawing code. With different APIs on the PC and PSP, I'm looking at some wrapper code for OpenGL and psp gu.
Hope this helps.
thing2k
For anyone who's interested, here's my open source, BSD-licenced GUI toolkit for the DS:
http://www.sourceforge.net/projects/woopsi
thing2k's answer is pretty good, but I'd seriously recommend having code to contain child UI elements in the base uiElement class. This is the pattern I've followed in Woopsi.
If you don't support this in the base class, you'll run into major problems when you try to implement anything more complex than a textbox and a button. For example:
Tab bars can be modelled as multiple buttons grouped together into a single parent UI element that enforces mutual exclusiveness of selection;
Radio button groups (ditto);
Scroll bars can be represented as a slider/gutter element and up/down buttons;
Scrolling lists can be represented as a container and multiple option UI elements.
Also, it's worth remembering that the DS has a 66MHz CPU and 4MB of RAM, which is used both to store your program and execute it (DS ROMs are loaded into RAM before they are run). You should really be treating it as an embedded system, which means the STL is out. I removed the STL from Woopsi and managed to save 0.5MB. Not a lot by desktop standards, but that's 1/8th of the DS' total available memory consumed by STL junk.
I've detailed the entire process of writing the UI on my blog:
http://ant.simianzombie.com/blog
It includes descriptions of the two algorithms I came up with for redrawing the screen, which is the trickiest part of creating a GUI (one just splits rectangles up and remembers visible regions; the other uses BSP trees, which is much more efficient and easier to understand), tips for optimisation, etc.
One useful strategy to keep in mind might be the composite pattern. At a low level, it might allow you to treat all GUI objects (and collections of objects) more easily once built. But I have no idea what's involved in GUI framework design, so one place to find general inspiration is in the source code of an existing project. WxWidgets is a cross-platform GUI framework with source available. Good luck with your project!
I think looking at the way other GUI toolkits have done it would be an excellent place to start. For C++ examples, I hear lots of good things about Qt. I haven't used it personally though. And of course WxWidgets as Nick mentioned.
I've written a very simple GUI just like you propose. I have it running on Windows, Linux and Macintosh. It should port relatively easily to any system like the PSP or DS too.
It's open-source, LGPL and is here:
http://code.google.com/p/kgui/