Qbasicnews.com

Full Version: float to int conversions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
ive made a particle type thing in qb which uses floating point for almost all calculations. Im trying to convert it to c++ but it wont work. i thought that maybe you cant use (allegros) putpixel with floating point x,y coords, so i did something like this:

Code:
float x,y;   //for calculations
int xx,yy;

xx=x;
yy=y;

putpixel (screen,xx,yy,color);
i obvioulsly get float to int warnings but it gives no errors.

it wont work though. I dont see any pixels on the screen at all.

CAN you use floating point with putpixel?
If not, how can i round it off to a interger value?
It converts it to int, and if you want to do it yourself you just do

putpixel (screen, (int)x,(int)y,color);

but i'm guessing something else is wrong.
It should work. Check if you are not going outside the screen boundaries. To avoid warnings, use:

Code:
xx = (int) x;

or Blitz's sollution as well.

that (int) thing is called "casting" and just does a type conversion.
casting got rid of the warnings (and made the code look better). but all it does is put the particles in there starting position, and nothing more. in qb you can watch a varible to see what it is doing, and i cant fiqure a way to do it in c++ (bloodshed devc++ & allegro). Im pretty new to c++ and seen something like

"number : %d", number

to print it to the screen, but i get to many arguments errors. maybe someone could show me more on the textout function?

ill try post the code tomorrow, but in the meantime you can check out the qb version in the general QB forum.
Sure, the code will help.
well, here the code. it's nothing special, i just converted my qb engine into c++ (aswell as i could at least):

Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

#include <allegro.h>

int main() {

   allegro_init();
   install_keyboard();

   set_color_depth(16);
   set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

   #define cX                   320
   #define cY                   240
   #define GRAVITY              0.1
   #define FRICTION             0.05
   #define MXV                  7
   #define MYV                  7
   #define MP                   99


   srand(time(NULL));

   int dir, dir2, t=1, f=0;

   struct ParticleType {
      float x, y, og, xv, yv;
      int doit;
   };
   ParticleType p[MP];

   for (int i; i<MP; i++) {
      p[i].x  = cX;
      p[i].y  = cY;
      p[i].xv = rand() % MXV;
      p[i].yv = rand() % MYV;
      p[i].og = rand() % 1;
      p[i].doit = TRUE;

      dir = rand() % 2;
      if (dir >= 1) {p[i].xv = p[i].xv * -1;}
      dir2 = rand() % 2;
      if (dir2 >= 1) {p[i].yv = p[i].yv * -1;}
   }

   while(!key[KEY_ESC]) {
      acquire_screen();
         for (int i; i < MP; i++) {
             if (p[i].doit == TRUE) {
                if (p[i].x >= 639) {p[i].doit = FALSE;}
                if (p[i].x <= 0 ) {p[i].doit = FALSE;}
                if (p[i].y >= 479) {p[i].doit = FALSE;}
                if (p[i].y <= 0 ) {p[i].doit = FALSE;}
                putpixel (screen, (int)p[i].x, (int)p[i].y, makecol(0,0,0));

                if (p[i].xv >= 0) {
                   p[i].y = p[i].y + p[i].yv;
                   if (p[i].yv < 0) {p[i].yv = p[i].yv + 0.1;}
                   p[i].y = p[i].y + GRAVITY + p[i].og;
                   p[i].x = p[i].x + p[i].xv;
                   p[i].xv = p[i].xv - FRICTION;
                   if (p[i].xv < 0) {p[i].xv = 0;}
                   putpixel (screen, (int)p[i].x, (int)p[i].y, makecol(0,255,0));
                }
                if (p[i].xv < 0) {
                   p[i].y = p[i].y + p[i].yv;
                   if (p[i].yv < 0) {p[i].yv = p[i].yv + 0.1;}
                   p[i].y = p[i].y + GRAVITY + p[i].og;
                   p[i].x = p[i].x + p[i].xv;
                   p[i].xv = p[i].xv + FRICTION;
                   if (p[i].xv > 0) {p[i].xv = 0;}
                   putpixel (screen, (int)p[i].x, (int)p[i].y, makecol(0,255,0));
                }
                p[i].og = p[i].og + 0.01;
             }
         }
      rest(10);
      release_screen();
   }
   return 0;
}
END_OF_MAIN();

it puts all the particles in their starting positions, and dosnt move them. I would asume that the velocitys and such arent being added to the x/y coords. why, i dont know. I dont know how to watch a varible so i cant be sure tho?

:x Cry
AFAIK, you don't need that release_screen and aquire_screen in the latest Allegro release. Maybe you are calling them so often and your video is getting screwed. Try moving them outside the loop.

I'll post later when I have five minutes to load up a project and your code and make some comprobations.
Ive only had a glance at your code, but here goes. Remember that #defines are basically just replacement text, which means that the code block:

Code:
p[i].x  = cX;
p[i].y  = cY;

Will look like this after preprocessing:

Code:
p[i].x = 320;
p[i].y = 240;

p[i].x is a float and you are assigning an integer to it, you could try either putting an explict cast here or changing the #defines to preprocess to floating point values. When assigning floating point values you should always put a decimal point in for portability (and for ansi C conformance) reasons, so instead of x = 1; you would have x = 1.0; Try changing all your floating point assignments to see if that fixes the problem.

Also some minor style points:
There is no reason to have structure definitions within a function (unless you really need them to be local to that particular function), you should have them independant of any function and somewhere near the top of the file, or better yet in a local header file. You can include local headers with #include "myheader.h".

Most preprocessors dont like indented # characters (ansi C forbids this), so instead of:
Code:
#define cX 320
You should have
Code:
#  define cX 320
Like struct definitions, you should place your defines either at the top of the file or in a separate header file to improve the readability and modifyiability of your code.

To monitor the values of your variables you could use the standard printf function as follows:
Code:
printf("Value of floating point variable: %2f\n", floatVar);
Will give the value of floatVar to 2 decimal places. Your IDE should provide a console of some sort that will allow you to see stdout while you have something like allegro taking control of the screen. C++ also has those ugly cout << thingee's but like Na_th_an I flat out refuse to learn C++ so I cant help you with those.
an easy way to output variables in bloodshed (and anything using g++ compilers) is the cout command;

it works like this

int x;

x=3;

cout<<x<<endl;

you can also output strings

cout<<"Hello WORLD!! "<<x<<" to the left is the value of x"<<endl;

endl flushes the buffer and puts a newline. also, something I have found useful for using floats in graphics is the following:

float y = 1.67;

y+=.5;

int yy=(int)y;

c normally just truncates floats to make ints, but if you add .5 before casting, it will round normally.
you forgot to initialize i in your for loop, so all of the variables are initialized to the default and not changed since the loop never executes

change it to this:


for (int i=0; i < MP; i++)
or whatever starting point your situation requires
Pages: 1 2