Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


GlenDC

Member Since 25 Jul 2012
Offline Last Active Aug 14 2014 04:14 PM

Topics I've Started

[SOLVED] Change function call order for a Layer (draw) system

27 September 2012 - 12:21 PM

I would like to design a system, which allows me to execute my functions in "layers". So a function of layer 3 should always be executed from a function of layer 4 but before a function of layer 2.
There are however some things the system has to be able to handle:
  • The layer order should be respected, not the call order for the user. The call order only is taken into account for functions within the same layer.
  • All the draw functions return void, BUT they all have different parameters. This means that not only the data type's of the function is different, but also the amount of parameters.
I'll explain on how i tried to implement this concept, but as I said before, I really think and hope there are better ways to do things like this as basically the core thing it has to do is change the order in when specific functions get called with specific functions. How I achieve this doesn't really matter as long as it can't become a bottleneck or hostile to the memory while used for games heavy on resources.

I've been working on this problem now for almost 3 days, and I still have not solved. The way I did it, was by saving all functions in a vector within a map. So every layer Is represented by one vector and these vectors are saved in a map. I've made some big Changes to my draw functions and had to create some extra things to achieve this. I think there have to be way better ways to transom this concept into C++ code. If not, I'll sum up the sort of components I have and where I do what. Every functor draw object is based on the Generic Function class:
Class Generic Function
{
protected:
GenericFunction() {}
public:
virtual void operator() (){}
virtual void operator() (boost::any){}
virtual void operator() (boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
virtual void operator() (boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any,boost::any){}
};
An example of a private draw function, saved in the list by the public function and based on the GenericFunction class:
struct dLine : public GenericFunction
  { virtual void operator() (float x0, float y0, float x1, float y1,float lineWidth = 1.0f, bool DoscaleOperations = true); };

The vector in the map doesn't contain a pointer to these functions, but a wrapper class, called Command:
(header)
class Command
{
public:
Command(GenericFunction * f,UINT nArgs, ...);
~Command();
void Execute();
private:
boost::shared_ptr<GenericFunction>  _f_;
std::vector<boost::any> _vaArguments_;
};
(implementation)
Command::Command(GenericFunction * f,UINT nArgs, ...) ctor
: _f_(f)
{
boost::any * it;
va_list args;
va_start(args,it);
for(unsigned int i = 0 ; i < nArgs ; ++i, ++it)
  _vaArguments_.push_back(&it);
va_end(args);
}
Command::~Command()
{
_f_.reset();
_vaArguments_.clear();
}
void Command::Execute()
{
auto it = _vaArguments_.begin();
switch(_vaArguments_.size())
{
case 0:
  boost::bind<void>(*(_f_.get()))();
  break;
case 1:
  boost::bind<void>(*(_f_.get()),_1)(*(it++));
  break;
case 2:
  boost::bind<void>(*(_f_.get()),_1,_2)(*(it++),*(it++));
  break;
case 3:
  boost::bind<void>(*(_f_.get()),_1,_2,_3)(*(it++),*(it++),*(it++));
  break;
case 4:
  boost::bind<void>(*(_f_.get()),_1,_2,_3,_4)(*(it++),*(it++),*(it++),*(it++));
  break;
case 5:
  boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5)(*(it++),*(it++),*(it++),*(it++),*(it++));
  break;
case 6:
  boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
  break;
case 7:
  boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6,_7)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
  break;
case 8:
  boost::bind<void>(*(_f_.get()),_1,_2,_3,_4,_5,_6,_7,_8)(*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++),*(it++));
  break;
}
}
The map that contains everything:
std::map<int,std::vector<boost::shared_ptr<Command> > > * _mPaintFunctionList_;
I reset this every tick, this because parameters have maybe changed:
if(_mPaintFunctionList_ != nullptr && _mPaintFunctionList_->size() != 0)
{
for ( auto it = _mPaintFunctionList_->begin() ; it != _mPaintFunctionList_->end() ; ++it )
{
  for ( auto iit = (*it).second.begin() ; iit != (*it).second.end() ; ++iit )
  {
   (*iit).reset();
  }
  (*it).second.clear();
}
_mPaintFunctionList_->clear();
}
When I want to paint my draw functions I call all the functions within that map:
if(_mPaintFunctionList_ != nullptr && _mPaintFunctionList_->size() != 0)
{
for ( auto it = _mPaintFunctionList_->begin() ; it != _mPaintFunctionList_->end() ; ++it )
{
  if((*it).second.size() != 0)
  {
   for ( auto iit = (*it).second.begin() ; iit != (*it).second.end() ; ++iit )
   {
	(*iit).get()->Execute();
   }
  }
}
}
An example on how I push such a function via a public function:
bool WinEngine::DrawLine(float x0, float y0, float x1, float y1, float lineWidth, UINT layer, bool DoscaleOperations)
{
if(_bIsPainting_ | _bIsDoubleBuffering_)
{
  (*_mPaintFunctionList_)[layer].push_back(
	boost::shared_ptr<Command>(new Command(new dLine(),6,x0,y0,x1,y1,lineWidth,DoscaleOperations))
  );
  return true;
}
_bPaintError_ = true;
return false;
}


That's basicly how the implementation is. It has the following problems:
  • The functions don't get executed, so something is wrong there.
  • have alot of functions who get added, ( you can't fix this problem, as I didnt share all my code ) while i don't see where they can be ever added to it ( I don't think I have ever called them )
Concerns:
  • I think this takes alot of memory ( even way to much )on the stack and heap, for something as trivial als executing a function
  • Maybe this can become a bottleneck when I have a lot of functions in a game where I have a lot of action and things that need to be on screen

One WIN32 Question and one General C++ Question

13 September 2012 - 07:23 AM

Hi, recently I wrote a topic about some problems with my win32 framework. Thanks to this awesome forum I was able to solve those problems. Now I have a new Question and an optional Question:

  • For one of my window modes I would like it that my window always has the same ratio when it's getting sized. Thus if you start with a size of 1280*720, that you can never go to another ratio when sizing. This prevents stretching while still being able to scale the content. So what's the best way on how doing it? I tried to do it and it works. However it doesn't work properly and you really see these ugly flickers. Code:
    
    if(abs(newWidth-_iWinWidth_) > abs(newHeight-_iWinHeight_)) newHeight = int((float)newWidth * _fWinRatioH_);
    
    else newWidth = int((float)newHeight * _fWinRatioW_);
    
    MoveWindow(_hWND_,rect->left,rect->top,newWidth,newHeight,true);
    
    break;
    
    
    newWidth is the width after the user resized the window, same for newHeight. _iWinHeight_ and _iWinWidth_ were the previous dimensions. (RECT)rect is init. by the GetClientRect(). So are there better methods to achieve this goal, or did I just make a mistake somewhere. ( Btw this code is done via a function, that function is executed whenever the window sized ( due to windows or a user action )
  • There is also another mode, I call it CLIENT_RATIO. This means that you can resize the window to whatever ratio you want, so you can just resize your window with no restrictions. However when you for example go from an original size of 1280*720 to 1400*720 then it will place vertical "banners" with a width of 60 left and right of the content. This so that your content will still be 1280*720. This to respect the ratio. I've tried to do this manually, but it is still bugged. I think I can get it working if I debug it. But still, I was wondering is there maybe something in WIN32 that allows this by default in a proper and correct way?

---------------------------------------------

Ok that was it for the WIN32 part, Now here's a real challenge.
The win32 framework is used for my 2d and 3d engine. I'm now trying to create a Console System, like you have in all the big games. ( e.g. UDK Games, Valve Games, ... ). I can create it and get it working, so what is my question? Well I would like to have a really fast way to add a function to the console function list, so that it allows me to do the following:

  • Type the function name and parameters in console textbox field
  • Get live code hinting while typing
  • Execute the function with the given parameters

Now I've thought of a system in which I give the function pointer and parameters manually, this is a really inconvenient way, but it works.
But Like I've said before, I want a faster way. Is it possible to just pass the function pointer and to extract than the parameters from the pointer of the function, so that you can put it in strings to get some sort of code hinting.

I've asked this question on IRC and these 2 solutions were given me:

  • Work with a sort of scripting language. This could be done for example via Python, which is possible thanks to the Boost Libraries.
  • Extract it from the pointer via stuff like function_traits ( also via the boost libraries )

I believe that both solutions can work. The problem is that I'm new to both solutions, so can someone give me some articles related to my solution in general, or if possible articles related to a solution using one of the 2 solutions listed above.

If any information I've given is too vague, then please ask for what you need more, and I shall try to provide it.
Thank you already! ( Oh and I'm traveling a lot these days between different locations, so it is possible that their is a long time period between my answers on this topic )


My custom WIN32 framework

06 September 2012 - 05:51 AM

Hi everyone, I believe this is my first topic, created by me. Helping people gives me great joy, but sadly enough I have a problem of my own at the moment, which I'm unable to solve. The solution is probably very simple and/or my mistakes are probably something stupid. But somehow I can't solve it. So that's why I need you guys! I still suck at expressing myself in English, I apologize for this. I'm working on it by speaking and writing it regularly, helping people at Gamedev and writing blog posts on my website.

In my University College we work via DirectX and WIN32 for our Windows PC Games. ( For some project we work with XNA, which enables us to create Xbox360 games). For my private windows projects I'm creating a WIN32 API based framework. So what do I already have and how do I do certain things?
  • Create very fast a win32 application with custom style settings, windows class settings, window general settings, etc...
  • Draw simple geometrical figures(normal or filled), polygons. Normal or filled), lines, etc...
  • Change window application settings dynamically at runtime
  • Draw Text
  • I paint everything via the Double buffering concept. I got that concept from here. I made some changes to it and extended it, thanks to the help of my prof.
Now what are my problems?
  • When I start the application the menu is hidden by default, it is only when I resize my window that the menu appears. From then on the menu stays as well.
  • After a short period, between 30 and 40 seconds, my entire window ( client rect + caption + ... ) gets filled with a static gray color. So the entire window is just that gray color. Nothing is disabled nor painted. So for example, you can still click the close (cross at the top right corner ) button to close the application.
Because of the size of my framework, and the many files + my own library (which is getting quite big) it is impossible to upload all my code. Here you can see the code which paints everything. If something is unclear or if you need more code related to a specific action, then please ask it. I'm trying to be as clear as possible.
Code from my WinMain part:
//..... a lot of code lines
//.... WinMain() {
//.....some code lines
#pragma warning ( disable : 4127 )
    while(true)
{
  if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
  {
   // Process the message
   if (msg.message == WM_QUIT) break;
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
  else // Game Cycle / OpenGl Cycle / DirectX Cycle
  {
   if(!WinEngine::GetSingleton()->GetSleep() && _bAppLoop_) //win engine aint sleeping
   {
    // Make sure the game engine isn't sleeping
    //Tick timing is independent from Paint timing
    _swpAppTimer_->Tick();
    double tickTimeNow = _swpAppTimer_->GetAppTime();
    if( tickTimeNow > GetFrameRate())
    {
	 //.... Keyboard stuff
	 RECT rect;
	 HDC hDC = GetDC(_hWND_);
	 GetClientRect(_hWND_, &rect);
	 //Double buffering code (c) Kevin Hoefman
	 HDC hBufferDC = CreateCompatibleDC(hDC);
	 //Create the bufffer
	 HBITMAP hBufferBmp = CreateCompatibleBitmap(hDC, GetWindowWidth(), GetWindowHeight());
	 HBITMAP hOldBmp = (HBITMAP) SelectObject(hBufferDC, hBufferBmp);
	 // user defined drawning
	 _hDC_ = hBufferDC;
	 _bIsDoubleBuffering_ = true;
	 _pApp_->AppTick(tickTimeNow);
	 _fpspApp_->Tick(tickTimeNow); // FPS timer...
	 UserPaint();
	 //UserPaint();
	 _bIsDoubleBuffering_ = false;
	 // As a last step copy the memdc to the hdc
	 BitBlt(hDC, 0, 0, _iWinWidth_, _iWinHeight_, hBufferDC, 0, 0, SRCCOPY);
	 //reset the old bmp of the buffer
	 SelectObject(hBufferDC,hOldBmp);
	 //kill the buffer
	 DeleteObject(hBufferBmp);
	 DeleteDC(hBufferDC);
	 //Release DC
	 ReleaseDC(_hWND_, _hDC_);
	 //reset Timer
	 _swpAppTimer_->Reset();
	 _swpAppTimer_->Start();
    }
    else
	 Sleep(1); // if no cpu cycle has ellpases sleep than for 1 ms
	    // doing this prevent the loop from hogging the cpu hog
   }
   else
    WaitMessage(); // If sleeping or if looping is disabled we just
	    // handle the window messages...
  }
}
#pragma warning ( default : 4127 )
//..... some code lines
// } // WinMain closes
//..... a lot of code lines

code from in my WM_PAINT message in my WndProc function
case WM_PAINT:
​//..... a lot of code
// .... WndProc {
// .. some code
   GetClientRect(_hWND_, &rect);
   nw = rect.right-rect.left; // new main window width
   nh = rect.bottom-rect.top; // new main window height
   if(_bDoubleBuffering_ == false)  //Single Buffer
   {
    _hDC_ = BeginPaint (hwnd, &ps);
    _bIsPainting_ = true;
    UserPaint();
    PaintRatioBorders(rect);
    _bIsPainting_ = false;
    EndPaint (hwnd, &ps);
   }
   else	    //Double buffer
   //http://www.gamedev.net/topic/411559-win32-double-buffering/
   {
    hdc = BeginPaint(hwnd,&ps);
  
    _hDC_ = CreateCompatibleDC(_hDC_);
    bitmap = CreateCompatibleBitmap(hdc,nw,nh);
    hOldBmp = (HBITMAP) SelectObject(_hDC_,bitmap);
    _bIsPainting_ = true;
    UserPaint();
    PaintRatioBorders(rect);
    _bIsPainting_ = false;
    BitBlt(hdc, 0, 0, nw, nh, _hDC_, 0, 0, SRCCOPY); // copy the memdc to the hdc
    SelectObject(_hDC_, hOldBmp); // reset old buffer.OldBMP
    // Kill buffer
    DeleteObject(bitmap);
    DeleteDC(_hDC_);
    // End paint
    EndPaint(_hWND_,&ps);
   }
   return 0;
// some code
// } // WndProc closes
//... a lot of code

Here you see what I mean with the second problem. (The problem which specifies that everything gets gray after a short period of running time ).
Posted Image
I hope you guys can help me. Thanks already for your time!

PARTNERS