Advertisement

a diferent way of programing

Started by July 03, 2001 09:22 PM
3 comments, last by The Alchemist 23 years, 7 months ago
is just something that i was thinking about lately normaly game code is something like:
  
GameMain
{
    getinput();
    collision();
    render();
    
 and it goes...
}
  
but sometimes(actualy all the time...) you wanna to change something, example you wanna to render the screen in wireframe, so normaly you do something like:
    
GameMain
{
   getinput();
   collision();
   render( type of render );

and it goes...
}
  
so it more-or-less means that:
        
render(type)
{
  if (type == WIREFRAME)
   {
     do wireframe...
   }
  else 
  {
and it goes...  
}
  
so in the end you have big number of "if"s, "else"s, "switch"s, etc... and lot of variables to controls those, things that are supose to eat resurces, so i came with this "way of programing" you make a array of function pointers, do a avaliation of the necessary functions( make lots of non-general functions. like: instead of render() do render_wireframe(), render_textured(),etc) and assign the pointers with they, and execute they in a loop util the program is over or there is need of a new avaliation of the functions (btw: all extra informatin is keep in globals) this way allows less "if"s in the code and open a lot of possibilities ( like: you could do a init_render() and a end_render(), and put opcional functions like render_particles(), render_actors(), render_that(), render_this(), etc in the middle. or maybe make diferent function arrays for redering, AI, sound, etc) but i know that im not a genius, or neither the first person to think in that, so why people don't use it? it makes something hard to program or anything else?- ------------------------------- MOTHERSHIP!!! hahaha! very funny! now take me outta here!!!!! Edited by - The Alchemist on July 3, 2001 10:27:29 PM
"Everything works out in the end, if it doesn't then it is not the end"
Well, suppose that you had, say, 10 different rendering options. If you used a switch statement, it would (should? I''m not exactly sure) compile to a jump table. So the question is ... how many CPU cycles does a single jump table access take? I would guess that it''s not too many.

IMHO, there are better places to optimize your code

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
Come to the darkside young one, learn the ways of OOD.

  class IRender{virtual void Render()=0;}class CRenderWireframe : public IRender{	virtual void Render()	{	//code to render wireframes...	}}class CRenderSolid : public IRender{	virtual void Render()	{	//Render groud shaded solids	}}class CRenderTextured : public IRender{	virtual void Render()	{	//Render with lighting & textures	}}IRender* g_pRenderer=0;Init(){g_pRenderer = new CRenderWireFrame;}SwitchRenderer(int Type){	delete g_pRenderer;	switch(Type)	//... new the appro. IRender subclass}GameMain(){	getinput();	collision();	g_pRenderer->Render();}  


you can have far more involved setups with more functions etc...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
There is a place to optimize, and a place to not worry about it. If it happens once a frame, and consumes a couple extra nanoseconds of CPU, then you ought to be spending your time looking for more effective optimizations, like the calculations which occur for thousands and tens of thousands of triangles per frame.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Or if you were using just C you could use function pointers like thus:

  void          (*Render)(struct *) = NULL;void SetRender(int RenderType)  {  switch (RenderType)    {    case RNDR_WIREFRAME:      Render = (void *)((char *)RenderWireframe);      break;    case RNDR_SOLID:      Render = (void *)((char *)RenderSolid);      break;    ... more cases here ...    }  return;  }  


And so your game flow will go like so:

  GameMain  {      getinput();      collision();      Render();     ...  }  



To actually change the render style you call SetRender() at some point within your progam, via a Quake-esque console or a keyhit.

HTH.


Stay Lucky, Graham "Mournblade" Reeds,
ICQ: 30514803
http://homepage.dtn.ntl.com/grahamr/

Edited by - grahamr on July 3, 2001 11:40:32 PM


Stay Lucky, Graham "Mournblade" Reeds,ICQ: 30514803http://homepage.dtn.ntl.com/grahamr/

This topic is closed to new replies.

Advertisement