Thread: speedup things

  1. #1
    Unregistered
    Guest

    Question speedup things

    How could one make the dos-screen draw faster ?
    I *think* that the length of the loop directly affects the execution time, no ?

    well here is my code (only the part that draws the screen), if u have any suggestions lets hear em

    Code:
    int draw()
    {
    	while(yc<=mym && xc<=mxm)
    	{
    		if(cm[xc][yc]=='\n')
    		{
    			printf("\n");
    			while(b!=0)
    			{
    				printf("\b");
    				b--;
    			}
    			yc++;
    			xc=0;
    		}
    		if(xc==xl && yc==yl && nm[xc][yc]==0)
    		{
    			textcolor(character);
    			cprintf("@");
    			nowrite=1;
    		}
    		if(flag==1)
    		{
    
    		}
    		else
    		{
    			switch(cm[xc][yc])
    			{
    				/* CONSTRUCTS */
    				case '#':
    				if(nocolor==0) textcolor(wall);
    				break;
    				case '.':
    				if(nocolor==0) textcolor(grass);
    				break;
    				case ',':
    				if(nocolor==0) textcolor(floor);
    				break;
    				case '~':
    				if(nocolor==0) textcolor(water);
    				break;
    				case '/':
    				if(nocolor==0) textcolor(door);
    				break;
    				case '+':
    				if(nocolor==0) textcolor(doorc);
    				nm[xc][yc]=2;
    				break;
    				case 'E':
    				if(nocolor==0) textcolor(enter);
    				break;
    				default:
    				textcolor(def);
    				break;
    			}
    		}
    		if(nowrite!=0);
    		else if(flag!=0) cprintf("%c",cm[xc-1][yc]);
    		else cprintf("%c",cm[xc][yc]);
    		xc++;
    		b++;
    		nowrite=0;
    		flag=0;
    	}
    	xc=0;
    	yc=0;
    	b=0;
    }
    thx in advance for any support

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(flag==1)
    {
    
    }
    else
    I'll assume you meant to do this for some vital reason.

    Code:
    switch(cm[xc][yc])
    			{
    				/* CONSTRUCTS */
    				case '#':
    				if(nocolor==0) textcolor(wall);
    				break;
    				case '.':
    				if(nocolor==0) textcolor(grass);
    				break;
    				case ',':
    				if(nocolor==0) textcolor(floor);
    				break;
    				case '~':
    				if(nocolor==0) textcolor(water);
    				break;
    				case '/':
    				if(nocolor==0) textcolor(door);
    				break;
    				case '+':
    				if(nocolor==0) textcolor(doorc);
    				nm[xc][yc]=2;
    				break;
    				case 'E':
    				if(nocolor==0) textcolor(enter);
    				break;
    				default:
    				textcolor(def);
    				break;
    This would be much better using a lookup table:

    Code:
    enum { L_wall, L_grass, L_water, L_door, L_doorc, L_enter, L_max };
    
    struct {
        char c;
        int color;
    } lookup[] =
    {
        { '#', wall },
        { '.', grass },
        { ',', floor},
        { '~', water },
        { '/', door },
        { '+', doorc },
        { 'e', enter },
        { 0, def },
    }
    Then you slightly modify how you store the data and use the lookup table to pull the correct character and color.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    Originally posted by quzah

    This would be much better using a lookup table:

    Code:
    enum { L_wall, L_grass, L_water, L_door, L_doorc, L_enter, L_max };
    
    struct {
        char c;
        int color;
    } lookup[] =
    {
        { '#', wall },
        { '.', grass },
        { ',', floor},
        { '~', water },
        { '/', door },
        { '+', doorc },
        { 'e', enter },
        { 0, def },
    }

    Could u please clarify this a bit or maybe a link to a tutorial ? im no expert in C... *yet*

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know what a structure is?
    Do you know what an array is?
    Do you know how to initialize a structure at declaration?
    Do you know how to initialize an array at declaration?

    This combines all of the above.

    First I made a structure. Since I didn't care if I ever made an instance of this structure, I didn't bother giving it a name. So, we have so far:

    Code:
    struct
    {
        char c;
        int i;
    }
    So you have a structure which contains a character and an integer.

    Then we tack on declaring an array, whose size we don't specify, called 'lookup'. This gives us:

    Code:
    struct
    {
        char c;
        int i;
    } lookup[] =
    Since it's size isn't declared, we have to initialize this when we create the variable. To initialize a structure at creation, you do this:

    struct <my struct name> <my var name> = { <structure elements> };

    So we end up with:

    Code:
    struct
    {
        char c;
        int i;
    } lookup[] = 
    {
        { <the character value>, <the integer value> },
         ... repeat for each additional structure you make ...
    
    };
    Got all that?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >enum { L_wall, <snip>
    ....
    >{ '#', wall },
    Are the enum names meant to match the names used in the array? (they have L_ missing)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >enum { L_wall, <snip>
    ....
    >{ '#', wall },
    Are the enum names meant to match the names used in the array? (they have L_ missing)
    Nope.

    Code:
    for( x = 0; x < L_max; x++ )
    {
        printf("%c, %d\n", lookup[x].c, lookup[x].i );
    }
    
    textcolor( lookup[L_door].i );
    cell = lookup[L_wall].c;
    He had his defines for colors, as such, you can't use those as simple enums, because their spot in the array more than likely doesn't match their corresponding numeric color value.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for things to study
    By Mastadex in forum Windows Programming
    Replies: 5
    Last Post: 08-18-2008, 09:23 AM
  2. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM
  3. things to think about... [from www.nuclearwastesite.com]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 12:47 AM
  4. What is your favorite things about programming?
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 10-21-2001, 12:13 PM
  5. Help with these three things...
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2001, 07:05 AM