Personal Framework

The Partridge Family were neither partridges nor a family. Discuss.
superstar1998
Posts: 52
Joined: March 10th, 2015, 7:51 pm
Location: Romania
Contact:

Personal Framework

Post by superstar1998 » May 27th, 2015, 5:59 pm

Hello again guys!

I decided to start making a new game, but i found myself in a deep need of buttons, text boxes and other stuff that would make the project really hard to make.
Therefore, i started making my own framework, based on what chili did and a few things from myself, which would allow me to create a game a lot easier. I decided to share it with you as some of you may make use of it, tell me if there is any bug (hard to test for everything) or help me improve it :)

What it contains from the tutorials:
-Surface - added functions to draw from right to left or from bottom to top
-SurfaceSequence - added an auto-advance option
-KeyboardClient - just the latest version i have, with single key presses

What I added:
-Button class - draws a button with some text, it can call functions from the game class via an id
-TextBox - I know windows has a textbox from itself, but i find it rather difficult to use, so I created one that fits our needs (or at least mine :D)
-ColorTransition - takes 2 D3DCOLOR colors and gives for every frame one between them, making the transition from the first to the last in a given nr of frames
-MColorTransitions - takes an array of D3DCOLOR and works just like the ColorTransition, just that it goes through multiple colors
-SurfaceEncrypter - takes a bmp file and transforms it into a smaller one, with all the necessary data (width, height, pixels) and nothing more; Surface can open them using a char* instead of a std::wstring parameter and also it's much faster to load; it shouldn't be included in the game, just ran in the program once so as to create the file, no need to call it every time you open the game

If there is anything you don't understand, fell free to ask, look over the code (in surface.h moslty) and the examples from the game class.

UPDATE:
-got rid of function pointers
-no more gamebuttons
-buttons now change color when mouse is over them
-button and textbox are now inside the game object and take an id
-the game class has a fuction which takes an id and chooses a function from inside the class to use
-small performance updates and a few more options for the textbox
-now backspace works properly in the textbox

NEW UPDATE:
-fixed a bug with the backspace in the textbox
-fixed an error in the surface when opening encripted files
-now the framework includes a program which helps you create or open an encripted file
-using the code written you can understand better how these new classes work
Attachments
Framework.rar
Latest version
(84.82 KiB) Downloaded 181 times
Last edited by superstar1998 on June 7th, 2015, 9:59 am, edited 5 times in total.
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”

– Martin Golding

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Personal Framework

Post by albinopapa » May 27th, 2015, 8:14 pm

Pretty neat idea and implementation.

Bug: Text box doesn't handle the backspace key, so you can't delete a character once it's typed.

Suggestion: All the new classes are in the Surface.h file and are inline, perhaps making separate files for the additional classes for clarity. Wouldn't have thought to look for the TextBox class where the Surface class would be. Also, the work has already been done on your part, so this may be a little pointless to say, but I'll say it anyway. Some of the params that you have passing to a few of the overloaded functions maybe a little excessive. For instance, don't see where users would have multiple D3DGraphics, MouseClient or Game objects so wouldn't need to pass as function params as they are already stored in the class and initialized through the constructors.

Suggestion: Buttons, I'm guessing would mostly be for accepting menu options or item purchases in game or something if you were to make an RPG etc. The use of function pointers as a default action for the constructed button make the user, depending on how many buttons, create an equal number of global functions making it more difficult to program as global function would need global pointers to access any objects. Unless the buttons are used specifically for menial tasks like adding some numbers or saving files, in other words, tasks that aren't related to specific classes. The GameButton is a good example of the problem.

Let's say you have a shop, where you have purchased a few items and now you press the "Buy" button. You have two options in code, you have the regular button that requires a global function and a GameButton that requires a global function and a game object function. We need to figure out what needs to happen when the button is pressed. When you buy items, they need to be added to your inventory and the cost of the items needs to be deducted from your purse.

Adding items to your inventory and subtracting cost from purse.
Well, here's the first problem, the global function pointers in the button class don't accept any params, so there's no way of handling the information, unless you must click on buy next to each item separately. In that case you have a global function for each "Buy" and/or "Sell" item button that deals specifically with that shop/screen. Even with the GameButton, you would have to do the same, as the only additional param is a pointer to the game class so you would have a global for each button instance, that calls a corresponding function in game.

There are a lot of reasons to not use function pointers in a framework such as this. Frankly, I'm not a fan of function pointers. They make the code hard to read for beginners, and harder to debug. The win32 api uses function pointers for things like the message loop and multithreading, because it's going to be calling those functions for you. They are called CALLBACK functions where you, the developer, are going to define how to handle a case where I, the API, call that function.

The message loop for instance, has 4 parameters, a handle to the window from which the message was sent, the id of the message which is an unsigned integer, a WPARAM which is a unsigned short that holds some meaning depending on the message and an LPARAM which is a LONG (not sure if signed or unsigned) which again depending on the message holds some meaning. The multithreading callback functions take a void*, in which you could pass a struct that holds the needed information such as a pointer to a class and any values needed to pass to the class function. If for instance you gave the buttons an ID and passed the button id along with action ids and the pointers of the items, you could forward that information to a function in the class that would examine the ids of the button pressed and use the action id of that button such as calling another function in a player object to subtract the cost from the player's purse and also calling a function that added the item pointers to the players inventory. That's beyond the scope of the framework I know, but preempting the needs of the framework users is what I'm getting at.

A framework needs to be functional on it's own, yet have the ability to be expanded without having to change the core functions of the framework. I applaud your efforts and the amount of work you have put into this must have been quite a journey. Keep it up, just try to keep in mind, if it's meant to be a framework, it needs to be as flexible as possible. If it's meant to be only used for a specific project, it can be whatever you want it to be.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Personal Framework

Post by albinopapa » May 27th, 2015, 8:57 pm

Thought I should also add that I really like the color transitions nice touch. I hope you take the prev post as constructive and not a dis. I truly like what I see and hope would like a fuller framework with more features. I can see this turning into a neat game maker (like RPG Maker) or something similar type framework.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

superstar1998
Posts: 52
Joined: March 10th, 2015, 7:51 pm
Location: Romania
Contact:

Re: Personal Framework

Post by superstar1998 » May 28th, 2015, 4:24 pm

Thanks albinopapa, it was sure a constructive post. The reason why I added so many functions for the buttons is the flexibility, thinking about what you could posibbly need so as to have it the easiest way. There are 2 functions already declared that do nothing and can be used if you don't want the button to handle functions for itself. It also has an option just to draw the button, without taking any specific action if left or right are pressed, so that you can do whatever you want with it. Also, I have a stop functions option which can give the button additional functionality without making the code look so bad.
I made this class mostly because it was really annoying to make buttons and add functionality for each of them, but i'll think of a better way to do it.

I guess I still have to work at the textbox, I have a few things in my mind right now so I'm on my way to implementing them :D
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”

– Martin Golding

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Personal Framework

Post by albinopapa » May 29th, 2015, 12:47 am

Awesome, now I just need a good idea for a game. I like to spend all my time coding and don't really spend a lot of time thinking of game ideas. Even if I have an idea or two, designing the whole thing is a beast or a chore. I mean, the story, the graphics, the dialog yada, yada.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Personal Framework

Post by chili » May 31st, 2015, 1:54 pm

Naked function pointers are pretty messy and hard to follow. Functors are better but still... my favorite way to handle custom behavior is virtual functions. You derive from the basic button class and then define specific behavior in the child class.

You can also use a message-based approach with on observable pattern. Use functors with the observable pattern to get something like c# delegation events. So many options...

I really like making custom window/control/event-based UI systems, and it's a practical skill that you need when making a game engine. Wanna cover that topic sometime (after hardware 3D is at least basically covered).
Chili

superstar1998
Posts: 52
Joined: March 10th, 2015, 7:51 pm
Location: Romania
Contact:

Re: Personal Framework

Post by superstar1998 » June 1st, 2015, 7:20 pm

Updated my framework a little, soon going to add a little app to see how this thing works.
Untill then, does anybody know how to create a wstring from a char* ? I really need this lease :)
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”

– Martin Golding

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Personal Framework

Post by cameron » June 2nd, 2015, 4:02 am

Can use MultibyteToWideChar
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Personal Framework

Post by albinopapa » June 2nd, 2015, 4:24 am

char = 1 byte
wchar = 2 byte

You can do as cameron suggested, which is probably the preferred mthod, or the basic ascii characters are in the low order byte or the wchar. So for instance char 'A' which is 0x41, wchar 'A' would be something like 0x0041.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Personal Framework

Post by LuisR14 » June 3rd, 2015, 3:18 am

albinopapa wrote:a WPARAM which is a unsigned short that holds some meaning depending on the message and an LPARAM which is a LONG (not sure if signed or unsigned)
both are 32-bit/64-bit types (depending on code arch hehe), only difference is WPARAM is unsigned (UINT_PTR), LPARAM is signed (LONG_PTR)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Post Reply