Saturday, March 31, 2012

Polymorphic Queue Problems

And now this doesn't work. I assume I'm going to have to do something involving "new" and "delete"...


 #include <iostream>  
 #include <queue>  
 using namespace std;  
 class thing1  
 {  
 public:  
   virtual void thingTest()  
   {  
     cout << "I AM THING 1\n";  
   }  
 };  
 class thing2: public thing1  
 {  
 public:  
    void thingTest()  
   {  
     cout << "I AM THING 2\n";  
   }  
 };  
 void DoMoreStuff( thing1& temp )  
 {  
      temp.thingTest();  
 }  
 queue <thing1> QueueThingy;  
 int main()  
 {  
      QueueThingy.push( thing2() );  
      QueueThingy.push( thing1() );  
      QueueThingy.push( thing2() );  
      QueueThingy.push( thing1() );  
      QueueThingy.push( thing2() );  
      while( QueueThingy.size() )  
      {  
           QueueThingy.front().thingTest();  
           QueueThingy.pop();  
      }  
 }  

Expected:
I AM THING 2
I AM THING 1
I AM THING 2
I AM THING 1
I AM THING 2

Actual:
I AM THING 1
I AM THING 1
I AM THING 1
I AM THING 1
I AM THING 1

Edit (thanks Matt):
Now I get expected... but I'm not TOTALLY convinced this doesn't cause a memory leak...

 #include <iostream>  
 #include <queue>  
 using namespace std;  
 class thing1  
 {  
 public:  
   virtual void thingTest()  
   {  
     cout << "I AM THING 1\n";  
   }  
 };  
 class thing2: public thing1  
 {  
 public:  
    void thingTest()  
   {  
     cout << "I AM THING 2\n";  
   }  
 };  
 void DoMoreStuff( thing1& temp )  
 {  
      temp.thingTest();  
 }  
 queue <thing1 * > QueueThingy;  
 void main()  
 {  
      QueueThingy.push( new thing2() );  
      QueueThingy.push( new thing1() );  
      QueueThingy.push( new thing2() );  
      QueueThingy.push( new thing1() );  
      QueueThingy.push( new thing2() );  
      while( QueueThingy.size() )  
      {  
           QueueThingy.front()->thingTest();  
           delete QueueThingy.front();  
           QueueThingy.pop();  
      }  
 }  

Friday, March 30, 2012

Weekend Started, so I fixed this:

http://stackoverflow.com/questions/9939739/simple-polymorphism

so with some help from Stack Overflow (and Matt, one of my only commenters; HI MATT!) , I did this stuff:

 #include <iostream>  
 using namespace std;  
 class thing1  
 {  
 public:  
   virtual void thingTest()  
   {  
     cout << "I AM THING 1\n";  
   }  
 };  
 class thing2: public thing1  
 {  
 public:  
    void thingTest()  
   {  
     cout << "I AM THING 2\n";  
   }  
 };  
 void DoStuff( thing1 temp )  
 {  
   temp.thingTest();  
 }  
 void DoMoreStuff( thing1& temp )  
 {  
      temp.thingTest();  
 }  
 int main()  
 {  
      //these look more like a thing1  
   DoStuff( thing2() );  
      ( (thing1) thing2() ).thingTest();  
      //look like thing2s  
      thing2().thingTest();  
      DoMoreStuff( thing2() );  
 }  

This outputs:
I AM THING 1
I AM THING 1
I AM THING 2
I AM THING 2
so, now to make the LinkedList to store Queues of Thing1s that could actually be Thing2s. (my render system will work by letting the coder dump things into a "todo list" and render the whole list at once)

Thursday, March 29, 2012

My Linked List

So... I can't figure out how to make this work. I'm pretty sure I have to do something involving pointers or something. My RenderSys will work on the same principle, so this totally needs to work. Anyway, here's what I'm having problems with:

 #include <iostream>  
 using namespace std;  
 class thing1  
 {  
 public:  
      void thingTest()  
      {  
           cout << "I AM THING 1!\n";  
      }  
 };  
 class thing2: public thing1  
 {  
 public:  
      void thingTest()  
      {  
           cout << "I AM THING 2\n";  
      }  
 };  
 void DoStuff( thing1 temp )  
 {  
      temp.thingTest();  
 }  
 void main()  
 {  
      DoStuff( thing2() );  
 }  

I expected it to output: 
"I AM THING 2"
But instead I got:
"I AM THING 1!"
clearly, I'm doing something wrong...

Protecting Myself

So, I've been reading all these stories online about tiny developers who make the interesting a new games only to have their games stolen quickly by bigger developers.

Now, I don't nearly have a game yet, BUT, someday I might. Right now I have a few building blocks of the massive structure of a working game. If some day I decide to sell it (or, more likely, give it away along with a "Donate" button), I will probably need to find a way to protect my code somehow.

The real problem here isn't that I think someone is going to steal my work and make money off it. That's fine, I don't mind if people do that at all (yeay free-flow of information!). The problem is that they could sue me if they copyright code based on mine, unless I can prove that I did it first. 

Also, just because I'm a coder and full of myself, I'd also like credit where it's due. If some one uses some of my code to build their awesome million dolor game (lol, no, that would never happen!), then I want credit (well, really I'd like a cut if I can get it, but w/e).

Licences I'm looking at:
Apache License
Creative Commons (they have a few for different uses)
CopyLeft
GNU General Public License
Any others that I missed?
Thoughts? (comments below)

Wednesday, March 28, 2012

Int to Char[4] and back again

I have decided to write a render system that creates a string class and as you add things to be rendered it add info to the string like a script. then when go to render, it pops off chars one at a time, reading it like a script and positioning objects on the screen based on their relative location the the camera definitions.  In order to do all this, I need a way to pass a int (4bytes) into 4 char values (1byte each).  Here is some example code I wrote to do just that:

 int CharsToInt( char top , char midt , char midb , char bot )  
 {  
      return(       
           (((int) top & 0xFF ) << 24) |   
           (((int) midt & 0xFF ) << 16) |   
           (((int) midb & 0xFF ) << 8) |   
           (((int) bot & 0xFF )) );  
 }  
 bool IntToChars( int INPUT , char *top , char *midt , char *midb , char *bot )  
 {  
      //going to rewrite this so that you just pass a pointer to a char[4].  
      //this would make passing from a String class easier  
      if( top && midt && midb && bot )  
      {  
           *top = (char) ( INPUT >> 24 ) & 0xFF;  
           *midt = (char) ( INPUT >> 16 ) & 0xFF;  
           *midb = (char) ( INPUT >> 8 ) & 0xFF;  
           *bot = (char) ( INPUT & 0xFF );  
           return true;  
      }  
      else  
           return false;  
 }  

EDIT: Rewrote it to be cleaner:

 int CharsToInt( char INPUT[] )  
 {  
      return(       
           (((int) INPUT[0] & 0xFF ) << 24) |   
           (((int) INPUT[1] & 0xFF ) << 16) |   
           (((int) INPUT[2] & 0xFF ) << 8) |  
           (((int) INPUT[3] & 0xFF )) );  
 }  
 bool IntToChars( int INPUT , char OUTPUT[] )  
 {  
      if( OUTPUT )  
      {  
           OUTPUT[0] = (char) ( INPUT >> 24 ) & 0xFF;  
           OUTPUT[1] = (char) ( INPUT >> 16 ) & 0xFF;  
           OUTPUT[2] = (char) ( INPUT >> 8 ) & 0xFF;  
           OUTPUT[3] = (char)  INPUT     & 0xFF;  
           return true;  
      }  
      else  
           return false;  
 }  

Wednesday, March 21, 2012

Render System Thoughts

I couldn't sleep, so I thought I'd jot some render system thoughts down.

I need to generate a render system that lives independently of my math system, but my math system has the power to dump data into the render system. I can't imagine a situation where my render system would need MathGraph logic, but it might happen.

The RenderSys I have in mind would have a list of things it is expected to do during a render cycle. Some of these commands might be:
DrawLine(  point , point , color );
DrawBox( point , point , color );
DrawSprite( string ObjectName , int SpriteFrame  );  [for now, my engine can remain vector-based, so sprites aren't strictly necessary]
To name a few. Every time something needs to be put on the screen from the main loop, what really happens  is a command is called that adds one of these to a queue. When the Render() function is called, it analyzes the render window, decides the dimensions of the that window, based on the it's location in the world and zoom, and then begins to dequeue the commands lined up during the rest of the program.

The main advantage to a system like this, is that rendering and game speed can run independently.  If the game should run at 200 cycles per second, then that is easily possible. The main loop would loop until a stopwatch function (called throughout the program, or after every main loop) says it's time to render again, and it takes the most recently completed RenderQueue, and then begins to operate on it.

This could also be used as a first step in multithreaded code. The main, more "mathy", loop (all the math of the game, with none of the graphics), could run on one thread, while the RenderSystem could run on a completely different thread. This would, again, mean that the main thread would run at exactly it's expected speed, but RenderSystem, would run as fast as it possibly can, taking the most recently completed RenderBuffer every time it runs. If the render system gets behind (ie, less than max frame rate), it doesn't hurt the main gameplay speed (if you aren't getting max frame rate, your game still appears to run at normal speed).

Finally, as a disclaimer, I've never done anything like this before (or even of this magnitude)... so I'm not even 100% sure I know if it would work they way I think it should or even how to create such a machine... we'll see...

And now I live on BlogSpot

I was using Tumblr, but I couldn't make it do the things I wanted to. In particular, I couldn't make it format code the way I wanted it to be formatted.

1:  //Scribbles.cpp  
2:  //if some one knows how to quote code better than this, I would be very happy  
3:  //http://www.mediafire.com/?ba62kkx78ft8p <-Scribbles.cpp and Scribbles.exe  
4:  //most of my code for now will probably use DarkGDK for now. it’s a very simple  
5:  //free c++ engine  
6:  #include “DarkGDK.h”   
7:  #define SCREENX 640  
8:  #define SCREENY 480  
9:  void Render()  
10:  {  
11:       dbText(0,0,”Press Space!”);  
12:       dbSync();  
13:  }  
14:  void DarkGDK ( void )  
15:  {  
16:       dbSyncOn  ( );  
17:       dbSyncRate ( 0 );  
18:       int x = dbRND(SCREENX);  
19:       int y = dbRND(SCREENY);  
20:       int LOOPS = 0;  
21:       // our main loop  
22:       while ( LoopGDK ( ) )  
23:       {  
24:            LOOPS++;  
25:            if(LOOPS == 60)  
26:            {  
27:                 Render();  
28:                 dbClear(0,0,0);  
29:                 LOOPS = 0;  
30:            }  
31:            int newX = x;  
32:            int newY = y;  
33:            x = dbRND(SCREENX);  
34:            y = dbRND(SCREENY);  
35:            dbInk(dbRGB(dbRND(256),dbRND(256),dbRND(256)),dbRGB(0,0,0));  
36:            dbLine(x,y,newX,newY);  
37:            if(dbKeyState(57))  
38:            {  
39:                 Render();  
40:            }  
41:       }  
42:       return;  
43:  }  

Tuesday, March 20, 2012

Cont' From Tumblr!

THIS BLOG WAS CONTINUED FROM TUMBLR

I couldn't make Tumblr format my code they way I wanted to, so I shifted over to blogger/blogspot. This will be the new blog, but if you want to see old posts, here it is:
http://iwasboredsoistartedcoding.tumblr.com/