My firts exampler C

Page 1/2
| 2

By aceki

Master (147)

aceki's picture

03-01-2018, 01:29

While it is a copy of some things, it is something I did understanding, the joke is the following:


#include 
#include 

/* Constantes */
#define logop (TINY) 0 
#define sp_trig (TINY) 1 

/* prototipos, definen la funciones antes de ser utilizados*/
VOID r_print();

static TINY cora[] = { 
0x18,0x24,0x24,0x18,0x24,0x5A,0x42,0x3C,
0x00,0x18,0x18,0x00,0x00,0x24,0x3C,0x00}; 

static TINY col[] = {
0x08,0x48,0x08,0x08,0x08,0x08,0x08,0x08,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x44,0x04,0x04,0x04,0x04,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 

VOID r_print(x,y) /* Funcion para el sprite*/
int x;
int y;
{
	glocate(x,y);
	inispr((TINY)2);
	sprite((TINY)0, cora);
	colspr((TINY)0, col);
	putspr((TINY)0, x, y,(TINY)0, (TINY)0);
}


main(){
	int i;
	int h;
	int joystickInput;
	screen((TINY)5); 
	ginit(); 
	color((TINY)15, (TINY)1, (TINY)1); 
	
	cls(); 
	
	i = 120;
	h = 120;
	
	joystickInput = 0;
	

	r_print(i,h);
	
 	while( 1 == 1) /* while infinito */
	/* aca empieza el movimiento del sprite*/
	{
		joystickInput = gtstck(0);
		if ( joystickInput == 3 )
		{
			i = i + 1;
			r_print(i,h); 
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 1 )
		{
			h = h - 2;
			r_print(i,h); 
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 5 )
		{
			h = h + 1;
			r_print(i,h); 
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 7 )
		{
			i = i - 2;
			r_print(i,h); 
		}
		joystickInput = 0;
		
		
	}
				
	return 0;
}

But, I have two problems two, the first one is the sprite refresh, I have a flicker every time I move it, how can I remove it? the second one, is the slowness of the advance, mainly in left and below, I need something more so that it does it to the speed of right and above?

Regards!!

Login or register to post comments

By ARTRAG

Enlighted (6285)

ARTRAG's picture

03-01-2018, 06:22

Put in r_print the sole putspr and move the other instructions in the main before the while()
Use switch() instead of multiple if

By Wolverine_nl

Paragon (1046)

Wolverine_nl's picture

03-01-2018, 12:15

also some initial variable declarations without values can be also before the main.

By Wolverine_nl

Paragon (1046)

Wolverine_nl's picture

03-01-2018, 14:42

Maybe an idea for a solution for the flickering;
Using r_print in the while loop, needs only be at the end 1 time, it cycles the while loop checking which button/joydirection is activated and then updates the r_print. Now it is doing that 4 times every cycle. That maybe one of the reasons for flickering.
I have a similair code, also a version with if's, and this one does not flicker and one of the differences is that is has only 1 sprite position update at the end.

Hope it helps, let us know. Smile

By aceki

Master (147)

aceki's picture

03-01-2018, 23:57

If I put it at the beginning or at the end of the while, I blink much faster by the fact that by not having a key pressed the loop is repeated by the condition of 1 == 1.

I would have to use another condition to execute the while, right?

By aceki

Master (147)

aceki's picture

04-01-2018, 00:32

EUREKA!!

As he said artarg and wolverine, the problem is the sprite, punctually repeat the entire sprite function, only with putscr is already perfect, the flicker is eliminated!

But ... there is still the issue that for right and up moves very fast and for left and down very slow ..

but we are improving, I leave the code as an example


#include 
#include 


/* Constantes */
#define logop (TINY) 0 
#define sp_trig (TINY) 1 

/* prototipos, definen la funciones antes de ser utilizados*/
VOID r_print();

static TINY cora[] = { 
0x18,0x24,0x24,0x18,0x24,0x5A,0x42,0x3C,
0x00,0x18,0x18,0x00,0x00,0x24,0x3C,0x00}; 

static TINY col[] = {
0x08,0x48,0x08,0x08,0x08,0x08,0x08,0x08,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x44,0x04,0x04,0x04,0x04,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 

VOID r_print(x,y) /* Funcion para el sprite*/
int x;
int y;
{
	glocate(x,y);
	inispr((TINY)2);
	sprite((TINY)0, cora);
	colspr((TINY)0, col);
	putspr((TINY)0, x, y,(TINY)0, (TINY)0);
}


main(){
	int i;
	int h;
	int joystickInput;
	screen((TINY)5); 
	ginit(); 
	color((TINY)15, (TINY)1, (TINY)1); 
	
	cls(); 
	
	i = 120;
	h = 120;
	
	joystickInput = 0;
	

	r_print(i,h);
	
 	while( 1 == 1) /* while infinito */
	
	{
		
		
		joystickInput = gtstck(0);
		
		if ( joystickInput == 3 )
		{
			i = i + 1;
			putspr((TINY)0, i, h,(TINY)0, (TINY)0);
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 1 )
		{
			h = h - 1;
			putspr((TINY)0, i, h,(TINY)0, (TINY)0);
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 5 )
		{
			h = h + 1;
			putspr((TINY)0, i, h,(TINY)0, (TINY)0);
		}
		joystickInput = 0;
		
		joystickInput = gtstck(0);
		if ( joystickInput == 7 )
		{
			i = i - 1;
			putspr((TINY)0, i, h,(TINY)0, (TINY)0);
		}
		joystickInput = 0;
		
		
		
		
	}
				
	return 0;
}

By ARTRAG

Enlighted (6285)

ARTRAG's picture

04-01-2018, 00:39

Try something like this


static TINY cora[] = {
0x18,0x24,0x24,0x18,0x24,0x5A,0x42,0x3C,
0x00,0x18,0x18,0x00,0x00,0x24,0x3C,0x00};

static TINY col[] = {
0x08,0x48,0x08,0x08,0x08,0x08,0x08,0x08,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x44,0x04,0x04,0x04,0x04,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};


main(){
	int i;
	int h;
	int joystickInput;
	screen((TINY)5);
	ginit();
	color((TINY)15, (TINY)1, (TINY)1);

	cls();

	i = 120;
	h = 120;

	joystickInput = 0;

	inispr((TINY)2);
	sprite((TINY)0, cora);
	colspr((TINY)0, col);

 	while( 1 == 1) /* while infinito */
	/* aca empieza el movimiento del sprite*/
	{
		joystickInput = gtstck(0);
		switch (joystickInput ) {
            case 3: {
                i ++;
                break;
            }
            case 1: {
                h --;
                break;
            }
            case 5: {
                h ++;
                break;
            }
            case 7: {
                i --;
                break;
            }

		}
        putspr((TINY)0, i, h,(TINY)0, (TINY)0);

        halt();

	}

	return 0;
}

By ARTRAG

Enlighted (6285)

ARTRAG's picture

04-01-2018, 00:42

I have no idea of which compiler or library you are using, BTW halt() is just the HALT instruction for pausing the z80
If you do not find it, you can omit, it but it will run like an hell

By aceki

Master (147)

aceki's picture

04-01-2018, 00:50

ARTRAG wrote:

I have no idea of which compiler or library you are using, BTW halt() is just the HALT instruction for pausing the z80
If you do not find it, you can omit, it but it will run like an hell

I am using msx-c 1.2 to compile.

With your example I always stay on one edge and it does not move anymore Sad

I had to comment on the halt since my compiler does not take it

By aceki

Master (147)

aceki's picture

04-01-2018, 00:56

add in the code


	while( 1 == 1) /* while infinito */
	
	{
		
		
		joystickInput = gtstck(0);
		
		switch (joystickInput ) {
            case 3: {
            	if (i == 256)
            	{           	
            		break;
				}
                i ++;
                break;
            }
            case 1: {
            	if (h == 0)
            	{
            		break;
            	}
                h --;
                break;
            }
            case 5: {
                if (h == 192 )
                {
                	break;
                }
				h ++;
                break;
            }
            case 7: {
				if  (i == 0) {
				break;
				}
				i --;
                break;
            }

		}
        putspr((TINY)0, i, h,(TINY)0, (TINY)0);

By Wolverine_nl

Paragon (1046)

Wolverine_nl's picture

04-01-2018, 10:08

Just something I have found in my old archive, maybe usefull, it is made for MSX-C v1.2
The code it crude, so it can be corrected on all fronts probably, but it works.
Mind, these functions are meant to be before the main() and in the main you can call them in a While loop (game loop).
Some declarations you have to make, it are snippets of functions just for the idea:

#define XMAX (TINY) 240 /* boundries screen movement player */
#define YMAX (TINY) 180 /* boundries screen movement player */
#define XMIN (TINY) 1 /* boundries screen movement player */
#define YMIN (TINY) 12 /* boundries screen movement player */
#define logop (TINY) 0
#define jport (TINY) 0
int px; /* player axis */
int py; /* player axis */
int min;
int max;


/* Player Sprite info */
VOID pl_sprite()
{
	sprite((TINY)11,plspra); 
	sprite((TINY)12,plsprb); 
	
	colspr((TINY)11,plclra); 
	colspr((TINY)12,plclrb); 
}

--
/* 			Player put sprite 			 */
/* ========================================= */	
VOID put_pl_sprite(px,py)
int px;
int py;
{
	pl_sprite();			
			putspr((TINY)11,px,py,logop,(TINY)11);
			putspr((TINY)12,px,py,logop,(TINY)12);
							
}

/* boundaries axis */
VOID in_range()
{
if (px > XMAX) 
	{px = XMAX;}
if (py > YMAX) 
		{py = YMAX;}
if (py < YMIN) 
		{py = YMIN;}
if (px < XMIN) 
		{px = XMIN;}
}

/* Player Controls */
VOID ctrl_pl_sprite()
{
min = 0; 
max = 16; 
for(min = 0;min < max; min++); 
			if (gtstck(jport) == 1) /* forward */
			{py = (py-1);} 
			else if (gtstck(jport) == 2) /* forward-right */
			{px = (px+1); py = (py-1); }
			else if (gtstck(jport) == 3) /* right */
			{px = (px+1); }
			else if (gtstck(jport) == 4) /* down-right */
			{px = (px+1);py=(py+1); }
			else if (gtstck(jport) == 5) /* down */
			{py = (py+1); }
			else if (gtstck(jport) == 6) /* down-left */
			{px = (px-1); py=(py+1);}
			else if (gtstck(jport) == 7) /* left */
			{px = (px-1); }
			else if (gtstck(jport) == 8) /* forward-left */
			{px = (px-1);py=(py-1);}
			put_pl_sprite(px,py);
			return(0);
}
Page 1/2
| 2