SDL's screen borer question

Started by
12 comments, last by diyemei 15 years, 4 months ago
frame_count=1 pos.x=508 pos.y=-16 v_x=-6 v_y=-21 loop();//pos.x+=v_x;pos.y+=v_y; frame_count=2 pos.x=502 pos.y=-21 ///??????? v_x=-6 v_y=-21 i draw a point at (pos.x,pos.y),and in the loop:pos.x+=v_x;pos.y+=v_y; then ,i redraw the point ,why the pos.y == -21? is the SDL take a number that below the zero for example -16 as the 0?(so that 0+(-21)=-21) anybody can make an explanation for me ?
Advertisement
Could you clarify a little bit of what your wanting to figure out (not very clear English *no offense!* :p) and tell us what your wanting to figure out, and you can put code like this:

[ source ]
code
[ /source ]

No spaces
It would seem very odd that -16 would suddenly change to 0. Are you absolutely sure that you do "pos.y += v_y" and not "pos.y = v_y" in your loop?

If there are no typos, can you tell us what kind of a structure/class pos is?
SDL_Rect pos;
int v_x,v_y;

of course i'm sure:pos.x+=v_x;pos.y+=v_y;
i just want to know:
when pos.y=-16 and v_y=-21
after pos.y+=v_y;
why pos.y = -21 ?

is the SDL take a number that below the zero for example -16 as the 0?
no answer?
I think juuso's got it right. SDL uses Sint16's for .x and .y of that struct, so negative values are valid. Can we get a look at exactly (copy and paste it here, between source tags) what your code is that changes the position?
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
Wow, you have been really patient!
As already said above, post your COMPLETE source code so we can help you.

By the way keep in mind that adding a negative number to another negative number doesn't make it positive (or zero).
For example:

tmp = -21;
tmp += -21;

tmp now is -42 and not 0.

Maybe that's the problem?
sorry,now ,i'll post the code ,
ps: i used it in DEV-C, and there's 2 balls in the screen ,and after you quit ,you could find the ball's positin

please give me the answer!!!

best regards!!!!!

code::

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <windows.h>
#include <SDL/SDL_image.h>
#define frame_max 10
#define g 8 //重力加速度

SDL_Surface *load_image(char *filename)
{
SDL_Surface *loadimage = NULL;
SDL_Surface *formatedimage = NULL;
Uint32 colorkey;

loadimage = IMG_Load( filename );

if(loadimage != NULL)
{
formatedimage = SDL_DisplayFormat(loadimage);
if(formatedimage !=NULL)
{
colorkey = SDL_MapRGB(formatedimage->format,0,0,0);
SDL_SetColorKey( formatedimage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
}


SDL_FreeSurface(loadimage);
}

return formatedimage;
}

int main(int argc,char *argv[])
{
int draw_x=500,draw_y=400;
SDL_Rect pos[3]; //5个球的坐标
int v_x[3],v_y[3]; //初速度
int frame_count=0; //表示第几帧
srand( (unsigned)time( NULL ) );

SDL_Surface *image[3];
SDL_Surface *screen = NULL;
SDL_Event event;

SDL_Init( SDL_INIT_EVERYTHING );

atexit(SDL_Quit);

screen = SDL_SetVideoMode(1024,768,0,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);

Uint32 colorkey = SDL_MapRGB(screen->format,0,0,0);
Uint8 *keystates = SDL_GetKeyState( NULL );

int i,j,k;

image[1] = load_image("./resourse/1.png"); //导入每个球的图
image[2] = load_image("./resourse/2.png");
//image[3] = load_image("./resourse/3.png");
//image[4] = load_image("./resourse/4.png");
//image[5] = load_image("./resourse/5.png");

/*for(i=6;i<51;i++)
{
j = i%5;
if(!j)image = image[5];
else if(j==1)image = image[1];
else if(j==2)image = image[2];
else if(j==3)image = image[3];
else if(j==4)image = image[4];
}

//image[6] = image[1];//load_image("./resourse/1.png");
//image[7] = image[2];//load_image("./resourse/2.png");
//image[8] = image[3];//load_image("./resourse/3.png");
//image[9] = image[4];//load_image("./resourse/4.png");
//image[10] = image[5];//load_image("./resourse/5.png");
//image[11] = image[1];//load_image("./resourse/1.png");
//image[12] = image[2];//load_image("./resourse/2.png");
//image[13] = image[3];//load_image("./resourse/3.png");
//image[14] = image[4];//load_image("./resourse/4.png");
//image[15] = image[5];//load_image("./resourse/5.png");
//image[16] = image[1];//load_image("./resourse/1.png");
//image[17] = image[2];//load_image("./resourse/2.png");
//image[18] = image[3];//load_image("./resourse/3.png");
//image[19] = image[4];//load_image("./resourse/4.png");
//image[20] = image[5];//load_image("./resourse/5.png");*/

for(i=1;i<3;i++) //初始化烟花起爆的坐标和初速度
{
pos.x=draw_x;
pos.y=draw_y;
if(i%2)
v_x = rand()%40 ;
else v_x = -rand()%40;
v_y = -rand()%90;
}

void play()
{
SDL_FillRect(screen,NULL,colorkey);
frame_count++;
if(frame_count<=frame_max)
{
for(i=1;i<3;i++)
{ //烟花起爆的坐标和初速度
SDL_BlitSurface(image,NULL,screen,&pos);
pos.x+=v_x;
pos.y+=v_y;
printf("frame_count=%d\n",frame_count);
printf("pos.x=%d\n",pos.x);
printf("pos.y=%d\n",pos.y);
v_y+=g;
printf("v_x=%d\n",v_x);
printf("v_y=%d\n",v_y);
printf("pos.x=%d\n",pos.x);
printf("pos.y=%d\n\n",pos.y);
}
SDL_Flip(screen);
}
else
{
frame_count=0;
//draw_x=rand()%650; //随机产生爆炸点
//draw_y=rand()%350;
for(i=1;i<3;i++) //重新初始化烟花起爆的坐标和Y轴初速度
{
pos.x=draw_x;
pos.y=draw_y;
if(i%2)
v_x = rand()%40 ;
else v_x = -rand()%40;
v_y = -rand()%90;
printf("frame_count=%d\n",frame_count);
printf("pos.x=%d\n",pos.x);
printf("pos.y=%d\n",pos.y);
printf("v_x=%d\n",v_x);
printf("v_y=%d\n",v_y);
printf("pos.x=%d\n",pos.x);
printf("pos.y=%d\n\n",pos.y);
}
}

SDL_Delay(300);
}

int display = 1;

while(display)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
break;
case SDL_MOUSEMOTION : //用鼠标位置作为烟花爆炸点
draw_x = event.motion.x-25;
draw_y = event.motion.y-25;
break;
case SDL_QUIT:
display = 0;
break;
}
}

if( keystates[SDLK_ESCAPE] ) break;//{display = 0;continue;}

play();

}
for(i=1;i<3;i++)
SDL_FreeSurface(image);
SDL_FreeSurface(screen);

SDL_Quit();

exit(0);
}
Have you changed any of the code since your original post?

Quote:Original post by diyemei
frame_count=1
pos.x=508
pos.y=-16
v_x=-6
v_y=-21

loop();//pos.x+=v_x;pos.y+=v_y;

frame_count=2
pos.x=502
pos.y=-21
v_x=-6
v_y=-21 // <--- should be -13 on this frame?


This can't be correct output. In your code, you have a loop that looks like this:
		for(i=1;i<3;i++) {			SDL_BlitSurface(image,NULL,screen, &pos);			pos.x+=v_x;			pos.y+=v_y;			printf("frame_count=%d\n",frame_count);			printf("pos.x=%d\n",pos.x);			printf("pos.y=%d\n",pos.y);                     			v_y+= g;   // <-- v_y += 8 every frame			printf("v_x=%d\n",v_x);			printf("v_y=%d\n",v_y);			printf("pos.x=%d\n",pos.x);			printf("pos.y=%d\n\n",pos.y);		}

g being a macro you wrote that represents some value 8. Your original post doesn't reflect the 8 that should've been added to v_y. Here's the output that I got:

frame_count=1pos.x=533pos.y=315v_x=33v_y=-77pos.x=533pos.y=315frame_count=1pos.x=497pos.y=352v_x=-3v_y=-40pos.x=497pos.y=352frame_count=2pos.x=566pos.y=238v_x=33v_y=-69pos.x=566pos.y=238frame_count=2pos.x=494pos.y=312v_x=-3v_y=-32pos.x=494pos.y=312et cetera


Can you provide any more information on what exactly the problem is? Are the positions wrong? Are the images not moving the way they should?
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
please attention: when the y = -2 or some other values blew the zero, and then y+=vy;
in the next frame, it will : y==vy, just as :in the last frame,y = 0(int fact y = -2)
why is it ??????

you can motion the mouse near the edge of the window,and check the stdout.txt

This topic is closed to new replies.

Advertisement