Jump to content

  • Log In with Google      Sign In   
  • Create Account

2d Game Creation



Computers...

Posted by , 31 October 2013 - - - - - - · 523 views
glReadPixels
So I cloned my main hard drive over to a bigger one and then extended the partition. Everything seemed fine the first day. I even programmed more. I have been working a lot at my sad non-computer based job so not much programming. I have been working on a multi-branching menu GUIObject. It was working....then the new(er) hard drive had a meltdown and windows went nuts. So we had to put the old hard drive in. I lost only a little bit of programming...and am somehow now not programming the menu right. For some reason glReadPixels reads the menu object as a whole entity. Then inside the menu object, I redraw the menu only, with each button and item inside having its own color. Now it is only reading the background color. Quite frustrating. It was SO much simplier when I did everything has a bounding box and checked if the mouse position was in or out of the box. But I want the possibility of interesting shapes for the GUIObjects so I am determined.

Still frustrated...

[Edit]
Decided to do a screw it. I figured it down the me trying to redo the frame/screen in which the menu object is located. Basically clear the area and redo the viewport. For some reason, this was messing up thing, because when I removed the code it started working just fine. So for now I am moving on, and will come back to it if I need to.


GUIObject: Button

Posted by , 16 October 2013 - - - - - - · 530 views
GUI
Now please correct me if I say something wrong. I am not trying to create a tutorial, but rather explain my process.

Now all my GUI (Graphical User Interface) objects inherit from GUIObject.hpp

#include <string>
#include "OpenGLContext.hpp"
#include "Command.hpp"
#include "AValue.hpp"
#include "Vector2i.hpp"
#include "Texture.hpp"
#include "FontBMP.hpp"
#include <vector>
#include <stdarg.h>
#include <stdio.h>
#include "Buffer.hpp"
#include "Mouse.hpp"


#ifndef GUIOBJECT_H
#define GUIOBJECT_H
class GUIObject
{
public:
    GUIObject()
    {
    };


    std::vector<Command> command; //outgoing command
    std::vector<Command> commands;  //command to copy to outgoing
    std::string name;
    std::string type;
    int width;
    int height;
    Vector2i position;
    Vector2i prevPos;
    bool enabled;  //Is this button clickable;
    bool visible;
    Vector2i mousePosition;
    int debug;
    std::string debugS;
    bool created;
    bool grabbed;


    virtual void Standard()
    {
        return;
    };
    virtual void CreateVAO()
    {
        return;
    }
    virtual void Destroy()
    {
        return;
    }
    virtual void Draw(std::vector<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext)
    {
        Draw(0, 0, textures, aFont, textured, openglContext);
        return;
    };
    virtual void Draw(int sx, int sy, std::vector<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext)
    {
        return;
    };
    virtual void DrawPick(int sx, int sy, Color4f thePick, OpenGLContext *openglContext)
    {
        return;
    }
    virtual void SetPosition(int x, int y, int Awidth, int Aheight)
    {
        position.x = x;
        position.y = y;
        width = Awidth;
        height = Aheight;
    }
    virtual void SetPosition(int x, int y)
    {
        SetPosition(x, y, width, height);
        return;
    }
    virtual bool InBounds(Vector2i pos)
    {
        return InBounds(pos.x, pos.y);
    }
    virtual bool InBounds(int x, int y)
    {
        if (x > position.x && x < position.x + width)
        {
            if (y > position.y && y < position.y + height)
            {
                return true;
            }
        }
        return false;
    }
    virtual bool InBounds(Vector2i pos, Vector2i upperLeft, Vector2i size)
    {
        if (pos.x > upperLeft.x && pos.x < upperLeft.x + size.x)
        {
            if (pos.y > upperLeft.y && pos.y < upperLeft.y + size.y)
            {
                return true;
            }
        }
        return false;
    }
    virtual bool ClickUp(Mouse& mouse)
    {
        return true;
    };
    virtual bool ClickHeld(Mouse& mouse)
    {
        return true;
    };
    virtual bool ClickDown(Mouse& mouse)
    {
        return true;
    };
    virtual int KeyboardInput(std::vector<bool>keys, std::vector<BufferLoc> buffer)
    {
        return -2;
    };
    virtual AValue PropertyGet(std::string theProp)
    {
        AValue newValue;
        newValue.theString = (char*)"none";
        return newValue;
    };
    virtual std::vector<AValue> PropertyGetVector(std::string theProp)
    {
        std::vector<AValue> newValue;
        newValue.clear();
        return newValue;
    };
    virtual void PropertySet(std::string theProp, std::string theFormat, ...)
    {
        return;
    };
    virtual void PropertySet(std::string theProp, AValue &theValue)
    {
        return;
    }
    virtual void Commands(Command &aCommand)
    {
        return;
    }
    virtual AValue CommandR(Command &aCommand)
    {
        return AValue();
    }
    virtual void LoseFocus()
    {
        return;
    }
};
#endif

Now my Button class will Inherit the GUIObject class. This allows Button to immediately have all the variables and functions that the GUIObject has. Plus, it can override any virtual class to make one with the same name and parameters, but different code inside it.
Now if I wanted to, I could make a function abstract versus virtual. This would make any class that inherits this class forced to override the function and make its own.

#ifndef Button_h
#define Button_h
#include "GUIObject.hpp"
#include "Quad.hpp"
#include <string>
class Button: public GUIObject
{
    public:
    Quad back;
    std::string text;
    Button()
    {
    }
    Button(const Button& other)
    {
        if (this != &other)
        {
            Destroy();
            back = other.back;
        }
    }

    void Standard();
    void CreateVAO();
    void Destroy();
    void Draw(std::vector<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext);
    void Draw(int sx, int sy, std::vector<Texture> &textures, FontBMP &aFont, bool textured, OpenGLContext *openglContext);
    void DrawPick(int sx, int sy, Color4f thePick, OpenGLContext *openglContext);
    void SetPosition(int x, int y, int Awidth, int Aheight);
    void SetPosition(int x, int y);
    bool InBounds(int x, int y);
    bool ClickUp(Mouse& mouse);
    bool ClickHeld(Mouse& mouse);
    bool ClickDown(Mouse& mouse);
    int KeyboardInput(std::vector<bool>keys, std::vector<BufferLoc> buffer);
    AValue PropertyGet(std::string theProp);
    std::vector<AValue> PropertyGetVector(std::string theProp);
    void PropertySet(std::string theProp, std::string theFormat, ...);
    void PropertySet(std::string theProp, AValue &theValue);
    void Commands(Command &aCommand);
    AValue CommandR(Command &aCommand);
    void LoseFocus();
};
#endif

Notice some of the functions are the same as GUIObject, just without the virtual part. These functions can be called even when stored in a GUIObject pointer, and will call the overridden function versus the GUIObject original function.

ComponentGUI has a vector 'std::vector<GUIObject*> guiObject' that stores a pointer to all the GUIObject created.
It has a function to Add a GUIObject:

int ComponentGui::AddControl(char *theName, char *theType, GUIObject *theObject)
{
    int count = AddControlVar();
    guiObject[count] = theObject;
    guiObject[count]->type = theType;
    guiObject[count]->name = theName;
    controlOrder.push_back(ControlOrder(count, theName));
    theObject = NULL;
    return count;
}
Now I want to add a button so I could call this function like this:
componentGui.AddControl("Button1", "Button", new Button());
Even though the parameter asked for two char* and a GUIObject pointer, I gave it two char* and a pointer to a Button. Since Button inherited GUIObject, the Button pointer can be used in place of a GUIObject pointer. This can work vice versa, but you have the type cast the GUIObject back to Button.
Button* button = (Button*)guiObject[0];
Now since guiObject[0] now contains a GUIObject, I can call one of its functions.
componentGui.guiObject[0]->Standard();
componentGui.guiObject[0]->Draw(0, 0, textures, aFont, textured, &openglContext);
This code calls Standard(), basically sets all the parameters of the object to starting/standard.
Then the next code draws it.
Now since button has overridden Standard and Draw, the buttons version of the functions will be called.

I do want to point out, you cannot call any function or use any parameter from Button that is not contained in GUIObject while it is thought of as a GUIobject.
'Button *button = new Button()' could call any of the functions from GUIObject or Button.
'GUIObject *guiObject = new Button()' could only call the functions from GUIObject. Now any overridden functions would be called instead, but they must have same name and parameters.


Component Manager and GUI

Posted by , 10 October 2013 - - - - - - · 601 views
GUI
So my custom GUI uses an inherited 'GUIObject.hpp'. They are then kept in a std::Vector of pointers. I have a ComponentManager that handles different screens/frames (however you want to look at it), the ComponentGui (cGui), and Component2d (c2d). Both cGui and c2d are 2d components, but the latter is going to handle the 2d objects and animations.
This time around, I am making sure every object has a constructor, a copy constructor, and an =operator override. Now obviously when translating from c++ to java for android, there will be changes. But a lot of use of the GUI is for the model/scene creation, and not the game itself.
The GUI before used to use mouse coordinates, a combination of left, middle, and right buttons as function parameters that is sent to the first GUI that the mouse coordinates were inside. The order of checking was determined by controlOrder, a vector variable that held a string and integer. It kept the position of guiObject[], and the type of object it was. I used a variable to order my controls and screens versus reordering the original vector because that seemed like the more efficient code. Changing the order of a complex variable would theoretically take more time than to change a variable that held an integer and string.
This time around, I am still using controlOrder to keep track of the drawing order. But, I am making sure that each guiObject[] has a Draw and a DrawPick function. The pick function takes one of its parameters as a pick color value. That value is then drawn in place of its normal color/texture value. I then use the glReadPixels to read a pixel value off the screen at the mouse coordinates.

R = Type. Is this a GUI_TYPE, a C2D_TYPE, SCREEN_TYPE (0 left as nothing for R)
G = Index1. Which GUI, C2D, SCREEN
B = Sub-Index. Which GUI Component, C2D Component, SCREEN part (control box, frame);

So far, this will limit me to 255 types, 256 indices, and 256 sub-indices. I am pretty sure I won't cry over limiting myself to 256 2d Objects on screen.

I have read so many articles, I couldn't tell you where I read how to do some of these things.


Beginnings

Posted by , 10 October 2013 - - - - - - · 532 views

I have started and abandons/restarted so many games/engines. Every since third grade, multitude of languages (QBasic, Visual Basic, C++, Java). I think in 20 years, I have completed 3 games (Tic Tac Toe, Dodger (a simple space craft moving right faster, astroids), and a simple 2d fighter). For the last 9 years I have restarted the same 3d engine with custom 2d GUI in opengl. Dont get me wrong, every time I restarted, I got farther and caught back up to my previous point with better code. It is really sad to realize how much time has gone by without finishing anything though.

So I am going to create a 2d game, but use skeletal animations. I want it a version on Android and PC.

So good luck to me.





October 2013 »

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  

Recent Entries

Recent Comments



PARTNERS