Gå til innhold

GravityMap (Var: Merkelig feilmelding fra GCC)


Anbefalte innlegg

Videoannonse
Annonse

Oppdatert koden! Jobber nå hovedsaklig med gravitasjonsakserelasjon -> fargeverdi-koden. I tillegg noe ny kode i main () for å finne max/min.

 

Utviklingen av matten bak følges her:

http://forum.hardware.no/index.php?showtop...0entry3962285

(inkluderer screenshots (2) og en bit av datasettet)

 

Hadde vært tøft om noen hadde tatt seg tid og bry til å kompilere det, så jeg vet at det ikke er noe problem. Kommandoen er:

gcc -Wall `sdl-config --cflags --libs` main.c -o gravitymap

 

Btw, å deaktivere den printf'en i hovedløkka gav ca. 50x mer hastighet :)

 

I alle fall - nok preik:

#include <stdlib.h>
#include <SDL.h>
#include <math.h>

//Resolution. Shold be autodetected...
#define ROWS 640
#define COLS 480

//Plotting stuff. Should eventually become dynamical...
// 6*10^8 / 640
#define METERPRPIXEL 937500
// 640 / 5
#define Xcenter 128
// 480 / 2
#define Ycenter 240

//Input values. MUST get dynamical ASAP, those here are just for testing!
//Object 1 (Earth) - 5,974*10^24
#define MASS1 5974000000000000000000000.0

//Natural constants
//Gravitational constant, 6,67*10^-11
#define GAMMA 0.0000000000667

//Functions
double grav_calc (double dx, double dy, double mass);
double grav_UnitConv (int grav_PixelValue);

Uint8 grav_colorConvR (double value);
Uint8 grav_colorConvG (double value);
Uint8 grav_colorConvB (double value);

void lock();
void unlock();
void drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b);
void setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b);



//Arrays to keep the info
double gravDepth1   [ROWS] [COLS];
double gravDepth2   [ROWS] [COLS];
double gravDepthSum [ROWS] [COLS];

//SDL stuff
SDL_Surface *screen;
SDL_Event event;

int main(int argc, char *argv[]) {
//Initialize SDL
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
 exit(1);
}
atexit(SDL_Quit);


screen = SDL_SetVideoMode(ROWS, COLS, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
 fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
 exit(1);
}

//Create some data!
printf ("Testing with the radius of the earth: %f \n", grav_calc (6371000,0,5974000000000000000000000.0));
int x;
int y;
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	//Popluate the array
 	gravDepth1 [x] [y] = grav_calc (grav_UnitConv(x - Xcenter), grav_UnitConv(y - Ycenter), MASS1);
 	
 	//This printf makes for a HUGE overhead. Better keep it commented out.
 	//printf ("Depth in (%d,%d):%f - colour: %d\n",x,y,gravDepth1 [x] [y], grav_colorConvR(gravDepth1 [x] [y]));
 	
 	//actually draw some stuff...
 	setpixel (x , y, grav_colorConvR(gravDepth1 [x] [y]),0, 0);
 	
 	//break;
 }
 //Make the line visible. Don't do this TO often - it has some speed overhead.
 SDL_Flip(screen);
 //break;
}

//Find max and min value
double max = 0;
double min = 0;
printf ("Finding max/min:\n");
//max:
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	if (gravDepth1 [x] [y] > max) {
   max = gravDepth1 [x] [y];
 	}
 }
}
printf ("max: %f\n", max);

//min
min = max;
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	if (gravDepth1 [x] [y] < min) {
   min = gravDepth1 [x] [y];
 	}
 }
}
printf ("min: %f\n", min);

//Wait for input
while(SDL_WaitEvent(&event)) {
 switch(event.type) {
 	case SDL_QUIT:
   printf ("QUIT!!\n");
   return (0);
 	break;
 	
 	/*
 	case SDL_KEYDOWN:
   switch (event.key.keysym.sym)
   	case SDLK_SPACE:
     //drawrect (50,50,200,200,255,255,255);
     printf ("SPACE!!\n");
     break;
   	//Why isn't this working? (i get a whole lot of error
   	//messages at compile time)
   	//I do not want to handle every key i don't use...
   	default:
     printf ("DEFAULT!!\n");
     break;
 	*/
 }
 SDL_Flip(screen);
}

return (0);
}

double grav_calc (double dx, double dy, double mass) {
//Hmm.. A problem with this formula, is that if r = 0, then it returns infinity. 
//Kindof makes the "max" finder a bit... borked. Oops..
return ((GAMMA * mass) / (dy*dy + dx*dx));
}

double grav_UnitConv (int grav_PixelValue) {
return (METERPRPIXEL * grav_PixelValue);
}

//Convert gravity depth value to a sane colour value
Uint8 grav_colorConvR (double value) {
//return (value * 255);
//return (log(value+1) * 1024);
//return (pow (value + 1, 0.5));
return ((log(value) + 1.847100) * 89);
}
Uint8 grav_colorConvG (double value) {
return 0;
}
Uint8 grav_colorConvB (double value) {
return 0;
}

//**************************************************************************
//********************** SDL DRAWING FUNCTIONS *****************************
//**************************************************************************
void lock()
{
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen)) 
{
if(SDL_LockSurface(screen) < 0) 
return;
}
}

void unlock()
{
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
}


void drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b)	{
Uint32 color;
color = SDL_MapRGB (screen->format, r, g, b);

SDL_Rect rect;
rect.w = x2;
rect.h = y2;
rect.x = x1;
rect.y = y1;
lock();
SDL_FillRect (screen, &rect, color);
unlock();
}



void setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) {
Uint8  *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;

lock();
/* Get the color */
color = SDL_MapRGB( screen->format, r, g, b );

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel) 
{
case 1: 
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x; 
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color; 
break;  

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);


if(SDL_BYTEORDER == SDL_LIL_ENDIAN) 
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else 
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);	
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

unlock();
}

Lenke til kommentar

Ny versjon av koden - og nå fungerer plottingen skikkelig! En skikkelig dustete feil - hadde byttet ut log10 med log... Men det er mulig at log vil gi en penere plotting. Å endre er uansett trivielt. Kode for å "autotilpasse" plotteren kommer snart uansett.

 

Fått noen nye idé'er for "hvordan gå videre" også :)

 

Se som vanlig:

http://forum.hardware.no/index.php?showtop...0entry3968081

 

Koden, GPL:

#include <stdlib.h>
#include <SDL.h>
#include <math.h>

//Resolution. Shold be autodetected...
#define ROWS 640
#define COLS 480

//Plotting stuff. Should eventually become dynamical...
// 6*10^8 / 640
#define METERPRPIXEL 937500
// 640 / 5
#define Xcenter 128
// 480 / 2
#define Ycenter 240

//Input values. MUST get dynamical ASAP, those here are just for testing!
//Object 1 (Earth) - 5,974*10^24
#define MASS1 5974000000000000000000000.0

//Natural constants
//Gravitational constant, 6,67*10^-11
#define GAMMA 0.0000000000667

//Functions
double grav_calc (double dx, double dy, double mass);
double grav_UnitConv (int grav_PixelValue);

Uint8 grav_colorConvR (double value);
Uint8 grav_colorConvG (double value);
Uint8 grav_colorConvB (double value);

void lock();
void unlock();
void drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b);
void setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b);



//Arrays to keep the info
double gravDepth1   [ROWS] [COLS];
double gravDepth2   [ROWS] [COLS];
double gravDepthSum [ROWS] [COLS];

//SDL stuff
SDL_Surface *screen;
SDL_Event event;

int main(int argc, char *argv[]) {
//Initialize SDL
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
 exit(1);
}
atexit(SDL_Quit);


screen = SDL_SetVideoMode(ROWS, COLS, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
 fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
 exit(1);
}

//Create some data!
printf ("Testing with the radius of the earth: %f \n", grav_calc (6371000,0,5974000000000000000000000.0));
int x;
int y;
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	//Popluate the array
 	gravDepth1 [x] [y] = grav_calc (grav_UnitConv(x - Xcenter), grav_UnitConv(y - Ycenter), MASS1);
 	
 	//This printf makes for a HUGE overhead. Better keep it commented out.
 	//printf ("Depth in (%d,%d):%f - colour: %d\n",x,y,gravDepth1 [x] [y], grav_colorConvR(gravDepth1 [x] [y]));
 	
 	//actually draw some stuff...
 	setpixel (x , y, grav_colorConvR(gravDepth1 [x] [y]),0, 0);
 	
 	//break;
 }
 //Make the line visible. Don't do this TO often - it has some speed overhead.
 SDL_Flip(screen);
 //break;
}

//Find max and min value
double max = 0;
double min = 0;
printf ("Finding max/min:\n");
//max:
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	if (gravDepth1 [x] [y] > max) {
   max = gravDepth1 [x] [y];
 	}
 }
}
printf ("max: %f\n", max);

//min
min = max;
for (x = 0; x < ROWS; x++) {
 for (y = 0; y < COLS; y++) {
 	if (gravDepth1 [x] [y] < min) {
   min = gravDepth1 [x] [y];
 	}
 }
}
printf ("min: %f\n", min);

//Wait for input
while(SDL_WaitEvent(&event)) {
 switch(event.type) {
 	case SDL_QUIT:
   printf ("QUIT!!\n");
   return (0);
 	break;
 	
 	/*
 	case SDL_KEYDOWN:
   switch (event.key.keysym.sym)
   	case SDLK_SPACE:
     //drawrect (50,50,200,200,255,255,255);
     printf ("SPACE!!\n");
     break;
   	//Why isn't this working? (i get a whole lot of error
   	//messages at compile time)
   	//I do not want to handle every key i don't use...
   	default:
     printf ("DEFAULT!!\n");
     break;
 	*/
 }
 SDL_Flip(screen);
}

return (0);
}

double grav_calc (double dx, double dy, double mass) {
//Hmm.. A problem with this formula, is that if r = 0, then it returns infinity. 
//Kindof makes the "max" finder a bit... borked. Oops..
return ((GAMMA * mass) / (dy*dy + dx*dx));
}

double grav_UnitConv (int grav_PixelValue) {
return (METERPRPIXEL * grav_PixelValue);
}

//Convert gravity depth value to a sane colour value
Uint8 grav_colorConvR (double value) {
//return (value * 255);
//return (log(value+1) * 1024);
//return (pow (value + 1, 0.5));
return ((log10(value) + 2.847100404) * 66);
}
Uint8 grav_colorConvG (double value) {
return 0;
}
Uint8 grav_colorConvB (double value) {
return 0;
}

//**************************************************************************
//********************** SDL DRAWING FUNCTIONS *****************************
//**************************************************************************
void lock()
{
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen)) 
{
if(SDL_LockSurface(screen) < 0) 
return;
}
}

void unlock()
{
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
}


void drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b)	{
Uint32 color;
color = SDL_MapRGB (screen->format, r, g, b);

SDL_Rect rect;
rect.w = x2;
rect.h = y2;
rect.x = x1;
rect.y = y1;
lock();
SDL_FillRect (screen, &rect, color);
unlock();
}



void setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) {
Uint8  *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;

lock();
/* Get the color */
color = SDL_MapRGB( screen->format, r, g, b );

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel) 
{
case 1: 
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x; 
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color; 
break;  

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);


if(SDL_BYTEORDER == SDL_LIL_ENDIAN) 
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else 
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);	
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

unlock();
}

 

Gleder meg til å titte på SDL_ttf. Takk for pekeren - jeg trodde dette var en del av "hoved-SDL".

Lenke til kommentar

Hirr :)

Flytte-byttet litt på koden -> fra å bruke 2 minutter bruker den nå 2 sekunder :D

 

#include <stdlib.h>
#include <SDL.h>
#include <math.h>

//Resolution. Shold be autodetected...
#define ROWS 640
#define COLS 480

//Plotting stuff. Should eventually become dynamical...
// 6*10^8 / 640
#define METERPRPIXEL 937500
// 640 / 5
#define Xcenter 128
// 480 / 2
#define Ycenter 240

//Input values. MUST get dynamical ASAP, those here are just for testing!
//Object 1 (Earth) - 5,974*10^24 kg, 6371 km
#define MASS1 5974000000000000000000000.0
#define RADIUS1 6371000

//Natural constants
//Gravitational constant, 6,67*10^-11
#define GAMMA 0.0000000000667

//Functions
double grav_calc (double dx, double dy, double mass);
double grav_UnitConv (int grav_PixelValue);

Uint8 grav_colorScale (double value);

void graph_SDL_init ();
void graph_lock();
void graph_unlock();
void graph_drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b);
void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b);



//Arrays to keep the info
double gravDepth1   [ROWS] [COLS];
double gravDepth2   [ROWS] [COLS];
double gravDepthSum [ROWS] [COLS];

//SDL stuff
SDL_Surface *screen;
SDL_Event event;

int main(int argc, char *argv[]) {

//Local variables, used for looping through the array
int x;
int y;

//Initalize SDL
graph_SDL_init ();

//Create some data!
//printf ("Testing with the radius of the earth: %f \n", grav_calc (6371000,0,5974000000000000000000000.0));

//Generate gravity data
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 //Popluate the array
 gravDepth1 [x] [y] = grav_calc (grav_UnitConv(x - Xcenter), grav_UnitConv(y - Ycenter), MASS1);
}
//if (ROWS % x == ROWS / 10) {
printf (".");
//}
}

//printf ("\n");

//Find max and min value
double max = 0;
double min = 0;
printf ("Finding max/min:\n");
//max:
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 if (gravDepth1 [x] [y] > max) {
  max = gravDepth1 [x] [y];
 }
}
}
printf ("max: %f\n", max);

//min
min = max;
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 if (gravDepth1 [x] [y] < min) {
  min = gravDepth1 [x] [y];
 }
}
}
printf ("min: %f\n", min);

//Draw!
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 graph_setpixel (x , y, grav_colorScale(gravDepth1 [x] [y]),0, 0);
}
printf(".");
}
printf("\n");
//Make the line visible. Don't do this TO often - it has some speed overhead.
SDL_Flip(screen);

//Wait for input
while(SDL_WaitEvent(&event)) {
switch(event.type) {
 case SDL_QUIT:
  printf ("QUIT!!\n");
  return (0);
 break;

 /*
 case SDL_KEYDOWN:
  switch (event.key.keysym.sym)
   case SDLK_SPACE:
    //drawrect (50,50,200,200,255,255,255);
    printf ("SPACE!!\n");
    break;
   //Why isn't this working? (i get a whole lot of error
   //messages at compile time)
   //I do not want to handle every key i don't use...
   default:
    printf ("DEFAULT!!\n");
    break;
 */
}
SDL_Flip(screen);
}

return (0);
}

double grav_calc (double dx, double dy, double mass) {
//Hmm.. A problem with this formula, is that if r = 0, then it returns infinity.
//Kindof makes the "max" finder a bit... borked. Oops..
return ((GAMMA * mass) / (dy*dy + dx*dx));
}

double grav_UnitConv (int grav_PixelValue) {
return (METERPRPIXEL * grav_PixelValue);
}

//Convert gravity depth value to a sane colour value
Uint8 grav_colorScale (double value) {
return ((log10(value) + 2.847100404) * 66);
}

//**************************************************************************
//********************** SDL DRAWING FUNCTIONS *****************************
//**************************************************************************
void graph_SDL_init () {
//Initialize SDL
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);


screen = SDL_SetVideoMode(ROWS, COLS, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
}

void graph_lock()
{
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0)
return;
}
}

void graph_unlock()
{
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
}


void graph_drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b) {
Uint32 color;
color = SDL_MapRGB (screen->format, r, g, b);

SDL_Rect rect;
rect.w = x2;
rect.h = y2;
rect.x = x1;
rect.y = y1;
graph_lock();
SDL_FillRect (screen, &rect, color);
graph_unlock();
}



void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) {
Uint8  *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;

graph_lock();
/* Get the color */
color = SDL_MapRGB( screen->format, r, g, b );

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x;
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;  

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);


if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

graph_unlock();
}

Lenke til kommentar

Har nå også implementert auto-tilpassning av farge-modifikatoren (de konstante verdiene i:

Uint8 grav_colorScale (double value) {
return ((log10(value) + 2.847100404) * 66);
}

er nå dynamiske, settes etter hvilke data vi generer.

 

Over til å bruke full vektorrepresentasjon!

I alle fall: i dag har jeg:

double gravDepth1   [ROWS] [COLS];

som er absoluttverdien. Men jeg ønsker for å lagre hele vektoren - også rettning! Altså må noe alá dette til:

struct gravDepth1   [ROWS] [COLS] {
double Xvec = 0;
double Yvec = 0;
};

Problemet er bare det at dette ikke er gyldig kode!

Feilmeldingene er:

main.c:49: warning: no semicolon at end of struct or union

main.c:49: error: syntax error before '=' token

main.c:51: error: syntax error before '}' token

 

Må jeg heller bruke noe alá dette?

struct gravDepth1 {
double Xvec [ROWS] [COLS] = 0;
double Yvec [ROWS] [COLS] = 0;
};

Vil dette fungere? Også initialiserings-systemet? Eller må jeg gjøre noe annet smart for å initialisere?

 

BTW dette er C - ikke C++, så class er utelukket. Ikke at dette er nødvendig heller, uansett. Jeg bare nevner det.

 

En annen ting: når jeg definerer en struct - lager jeg da en ny datatype, som jeg så må lage en "inkarnasjon" av?

Eller lager jeg en variabel direkte?

Lenke til kommentar

Ny versjon - nå vektorisert!

 

Men en liten ting jeg stusser over - jeg forsøker å dumpe noen av dataene fra arrayet (sånn bare for å teste at jeg har regna riktig...) - men ingenting kommer tilbake! Selv ikke de dataene jeg senere bruker til å tegne...

 

I alle fall - kode:

/*
Gravitymap - makes a map of gravitational fields
Copyright (C) 2005  Kyrre Ness Sjøbæk
Portions  (C) 2004  Thomas Adamcik	(some drawing routines)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <stdlib.h>
#include <SDL.h>
#include <math.h>

//Resolution. Shold be autodetected...
#define ROWS 640
#define COLS 480

#define COLOR_MAX 255

//Plotting stuff. Should eventually become dynamical...
// 6*10^8 / 640
#define METERPRPIXEL 937500
// 640 / 5
#define Xcenter 128
// 480 / 2
#define Ycenter 240

//Input values. MUST get dynamical ASAP, those here are just for testing!
//Object 1 (Earth) - 5,974*10^24 kg, 6371 km
#define MASS1 5974000000000000000000000.0
#define RADIUS1 6371000

//Natural constants
//Gravitational constant, 6,67*10^-11
#define GAMMA 0.0000000000667

//Functions
double grav_calcScalar (double dx, double dy, double mass);
double grav_UnitConv (int grav_PixelValue);

Uint8 grav_colorScale (double value);
void grav_setColorScale (double max, double min);

void graph_SDL_init ();
void graph_lock();
void graph_unlock();
void graph_drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b);
void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b);



//Arrays to keep the info
//double gravDepth1   [ROWS] [COLS];
double gravDepth2   [ROWS] [COLS];
double gravDepthSum [ROWS] [COLS];

typedef struct grav_data {
double Xvec;
double Yvec;
double scalar;
double angle;
} grav_data;

grav_data gravDepth1 [ROWS] [COLS];

//SDL stuff
SDL_Surface *screen;
SDL_Event event;

//Some global vars:
double grav_scaleFactorA = 0;
double grav_scaleFactorB = 0;

int main(int argc, char *argv[]) {

//Local variables, used for looping through the array
int x;
int y;

double dx;
double dy;

//Initalize SDL
graph_SDL_init ();

//Create some data!
//printf ("Testing with the radius of the earth: %f \n", grav_calc (6371000,0,5974000000000000000000000.0));

//Generate gravity data
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 dx = Xcenter - x;
 dy = Ycenter - y;
 //Popluate the array
 gravDepth1[x] [y].scalar = grav_calcScalar (grav_UnitConv(dx), grav_UnitConv(dy), MASS1);
 gravDepth1[x] [y].angle = atan (dx / dy);
 gravDepth1[x] [y].Xvec = gravDepth1[x] [y].scalar * sin (gravDepth1[x] [y].angle);
 gravDepth1[x] [y].Yvec = gravDepth1[x] [y].scalar * cos (gravDepth1[x] [y].angle);
}
printf ("POS: (%d,%d), Scalar: %f, Angle: %f, Vector: [%f,%f]\n", x, y, gravDepth1[x] [y].scalar, gravDepth1[x] [y].angle, gravDepth1[x] [y].Xvec, gravDepth1[x] [y].Yvec);
}

//Find max and min value
double max = 0;
double min = 0;

//max:
max = grav_calcScalar (RADIUS1,0,MASS1);
printf ("max: %f\n", max);

//min
min = max;
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 if (gravDepth1 [x] [y].scalar < min) {
  min = gravDepth1 [x] [y].scalar;
 }
}
}
printf ("min: %f\n", min);

//Draw!
grav_setColorScale (max, min);

for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 graph_setpixel (x , y, grav_colorScale(gravDepth1 [x] [y].scalar),0, 0);
}
}

//Make the line visible. Don't do this TO often - it has some speed overhead.
SDL_Flip(screen);

//Wait for input
while(SDL_WaitEvent(&event)) {
switch(event.type) {
 case SDL_QUIT:
  printf ("QUIT!!\n");
  return (0);
 break;

 /*
 case SDL_KEYDOWN:
  switch (event.key.keysym.sym)
   case SDLK_SPACE:
    //drawrect (50,50,200,200,255,255,255);
    printf ("SPACE!!\n");
    break;
   //Why isn't this working? (i get a whole lot of error
   //messages at compile time)
   //I do not want to handle every key i don't use...
   default:
    printf ("DEFAULT!!\n");
    break;
 */
}
SDL_Flip(screen);
}

return (0);
}

double grav_calcScalar (double dx, double dy, double mass) {
//Hmm.. A problem with this formula, is that if r = 0, then it returns infinity.
//Kindof makes the "max" finder a bit... borked. Oops..
return ((GAMMA * mass) / (dy*dy + dx*dx));
}

double grav_UnitConv (int grav_PixelValue) {
return (METERPRPIXEL * grav_PixelValue);
}

//Convert gravity depth value to a sane colour value
Uint8 grav_colorScale (double value) {
//return ((log10(value) + 2.847100404) * 66);
return ((log10(value) + grav_scaleFactorA) * grav_scaleFactorB);
}

void grav_setColorScale (double max, double min) {
grav_scaleFactorA = -log10(min); //How will this behave if min > 1 ?
grav_scaleFactorB = COLOR_MAX / (log10(max) + grav_scaleFactorA);
}
//**************************************************************************
//********************** SDL DRAWING FUNCTIONS *****************************
//**************************************************************************
void graph_SDL_init () {
//Initialize SDL
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);


screen = SDL_SetVideoMode(ROWS, COLS, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
}

void graph_lock()
{
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0)
return;
}
}

void graph_unlock()
{
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
}


void graph_drawrect(int x1, int y1, int x2, int y2, Uint8 r, Uint8 g, Uint8 b) {
Uint32 color;
color = SDL_MapRGB (screen->format, r, g, b);

SDL_Rect rect;
rect.w = x2;
rect.h = y2;
rect.x = x1;
rect.y = y1;
graph_lock();
SDL_FillRect (screen, &rect, color);
graph_unlock();
}



void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) {
Uint8  *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;

graph_lock();
/* Get the color */
color = SDL_MapRGB( screen->format, r, g, b );

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x;
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;  

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);


if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

graph_unlock();
}

Lenke til kommentar

Whoho! Nå begynner ting å komme seg! Lagt til en ekstra grafikkmotor (vektorlinjer, men som dere kan se av screenshot'et, så virker den ikke så bra ennå ;) )

 

Jeg vet dog hvordan man fikser den.

 

int main(int argc, char *argv[])

 

Er det noen som vet hvordan man bruker disse? Jeg ønsker å sjekke for --help og --drawmethod {1 | 0}

 

Fant denne koden:

#include<stdio.h>

int main(int argc, char *argv[])
{
 int loop;

 if(argc>0)
   printf("My program name is %s.\n",argv[0]);
 
 if(argc>1)
  {
   for(loop=1;loop<argc;loop++)
     printf("Parameter #%i is %s.\n",loop,argv[loop]);
  }
}

 

Men den er litt enkel. Poster den likevel her for min egen referanse, fant den på:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.3.html

post-41-1113339036_thumb.png

Lenke til kommentar

Hadde tenkt å legge ved koden, men det var tydeligvis ikke lov å laste opp .cpp filer. Så da legger vi den her:

 

main.cpp:

/*
Gravitymap - makes a map of gravitational fields
Copyright (C) 2005  Kyrre Ness Sjøbæk
Portions  (C) 2004  Thomas Adamcik	(some drawing routines)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include <stdlib.h>
#include <SDL.h>
#include <math.h>
#include <string.h>

//Resolution. Shold be autodetected...
#define ROWS 640
#define COLS 480

#define COLOR_MAX 255

//Plotting stuff. Should eventually become dynamical...
// 6*10^8 / 640
#define METERPRPIXEL 937500
// 640 / 5
#define Xcenter 128
// 480 / 2
#define Ycenter 240

//Input values. MUST get dynamical ASAP, those here are just for testing!
//Object 1 (Earth) - 5,974*10^24 kg, 6371 km
#define MASS1 5974000000000000000000000.0
#define RADIUS1 6371000

//Natural constants
//Gravitational constant, 6,67*10^-11
#define GAMMA 0.0000000000667

//Functions
double grav_calcScalar (double dx, double dy, double mass);
double grav_UnitConv (int grav_PixelValue);

Uint8 grav_colorScale (double value);
void grav_setColorScale (double max, double min);

void graph_SDL_init ();
void graph_lock();
void graph_unlock();
void graph_drawrect(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b);
void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b);

void line(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color);


//Arrays to keep the info
//double gravDepth1   [ROWS] [COLS];
double gravDepth2   [ROWS] [COLS];
double gravDepthSum [ROWS] [COLS];

typedef struct grav_data {
double Xvec;
double Yvec;
double scalar;
double angle;
} grav_data;

grav_data gravDepth1 [ROWS] [COLS];

//SDL stuff
SDL_Surface *screen;
SDL_Event event;

//Some global vars:
double grav_scaleFactorA = 0;
double grav_scaleFactorB = 0;

int main(int argc, char *argv[]) {

//There should be some code here checking for arguments
int drawmethod = 0;

//yeah... detected....
drawmethod = 2;


//Local variables, used for looping through the array
int x;
int y;

double dx;
double dy;

//Initalize SDL
graph_SDL_init ();

//Create some data!
//printf ("Testing with the radius of the earth: %f \n", grav_calc (6371000,0,5974000000000000000000000.0));

//Generate gravity data
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 dx = Xcenter - x;
 dy = Ycenter - y;
 //Popluate the array
 gravDepth1[x] [y].scalar = grav_calcScalar (grav_UnitConv(dx), grav_UnitConv(dy), MASS1);
 gravDepth1[x] [y].angle = atan (dx / dy);
 gravDepth1[x] [y].Xvec = gravDepth1[x] [y].scalar * sin (gravDepth1[x] [y].angle);
 gravDepth1[x] [y].Yvec = gravDepth1[x] [y].scalar * cos (gravDepth1[x] [y].angle);
}
//printf ("POS: (%d,%d), Scalar: %f, Angle: %f, Vector: [%f,%f]\n", x, y, gravDepth1[x] [y].scalar, gravDepth1[x] [y].angle, gravDepth1[x] [y].Xvec, gravDepth1[x] [y].Yvec); //Debugging. Didn't work anyway.
}

//Find max and min value
double max = 0;
double min = 0;

//max:
max = grav_calcScalar (RADIUS1,0,MASS1);
printf ("max: %f\n", max);

//min
min = max;
for (x = 0; x < ROWS; x++) {
for (y = 0; y < COLS; y++) {
 if (gravDepth1 [x] [y].scalar < min) {
  min = gravDepth1 [x] [y].scalar;
 }
}
}
printf ("min: %f\n", min);

//Draw!
switch (drawmethod) {
case 1:        	// Standard scalar display
 grav_setColorScale (max, min);
 
 for (x = 0; x < ROWS; x++) {
   for (y = 0; y < COLS; y++) {
     graph_setpixel (x , y, grav_colorScale(gravDepth1 [x] [y].scalar),0, 0);
   }
 }
break;

case 2:        	// Vector arrow display
 for (x = 20; x < ROWS; x = x + 40) {
 	for (y = 20; y < COLS; y = y + 40) {
   graph_drawrect (x-3, y-3, 6, 6, 0, 255, 0);
   line (screen, x, y, gravDepth1 [x] [y].Xvec, gravDepth1 [x] [y].Yvec, SDL_MapRGB (screen->format, 255, 0, 0));
 	}
 }
 SDL_Flip(screen);
break;

default:
 printf ("unknow drawmethod, exiting\n");
 return 1;
 break;	//Not veeeery neccesary...
}
//Make the line visible. Don't do this TO often - it has some speed overhead.
SDL_Flip(screen);

//Wait for input
while(SDL_WaitEvent(&event)) {
switch(event.type) {
 case SDL_QUIT:
  printf ("QUIT!!\n");
  return (0);
 break;

 /*
 case SDL_KEYDOWN:
  switch (event.key.keysym.sym)
   case SDLK_SPACE:
    //drawrect (50,50,200,200,255,255,255);
    printf ("SPACE!!\n");
    break;
   //Why isn't this working? (i get a whole lot of error
   //messages at compile time)
   //I do not want to handle every key i don't use...
   default:
    printf ("DEFAULT!!\n");
    break;
 */
}
SDL_Flip(screen);
}

return (0);
}

double grav_calcScalar (double dx, double dy, double mass) {
//Hmm.. A problem with this formula, is that if r = 0, then it returns infinity.
//Kindof makes the "max" finder a bit... borked. Oops..
return ((GAMMA * mass) / (dy*dy + dx*dx));
}

double grav_UnitConv (int grav_PixelValue) {
return (METERPRPIXEL * grav_PixelValue);
}

//Convert gravity depth value to a sane colour value
Uint8 grav_colorScale (double value) {
//return ((log10(value) + 2.847100404) * 66);
return ((log10(value) + grav_scaleFactorA) * grav_scaleFactorB);
}

void grav_setColorScale (double max, double min) {
grav_scaleFactorA = -log10(min); //How will this behave if min > 1 ?
grav_scaleFactorB = COLOR_MAX / (log10(max) + grav_scaleFactorA);
}
//**************************************************************************
//********************** SDL DRAWING FUNCTIONS *****************************
//**************************************************************************
void graph_SDL_init () {
//Initialize SDL
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);


screen = SDL_SetVideoMode(ROWS, COLS, 16, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
}

void graph_lock()
{
/* Lock the screen, if needed */
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0)
return;
}
}

void graph_unlock()
{
/* Unlock the screen if needed */
if(SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
}


void graph_drawrect(int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
Uint32 color;
color = SDL_MapRGB (screen->format, r, g, b);

SDL_Rect rect;
rect.w = w;
rect.h = h;
rect.x = x;
rect.y = y;
graph_lock();
SDL_FillRect (screen, &rect, color);
graph_unlock();
}



void graph_setpixel(int x, int y, Uint8 r, Uint8 g, Uint8 b) {
Uint8  *ubuff8;
Uint16 *ubuff16;
Uint32 *ubuff32;
Uint32 color;
char c1, c2, c3;

graph_lock();
/* Get the color */
color = SDL_MapRGB( screen->format, r, g, b );

/* How we draw the pixel depends on the bitdepth */
switch(screen->format->BytesPerPixel)
{
case 1:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + x;
*ubuff8 = (Uint8) color;
break;

case 2:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*2);
ubuff16 = (Uint16*) ubuff8;
*ubuff16 = (Uint16) color;
break;  

case 3:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y * screen->pitch) + (x*3);


if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
c1 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c3 = (color & 0x0000FF);
} else
{
c3 = (color & 0xFF0000) >> 16;
c2 = (color & 0x00FF00) >> 8;
c1 = (color & 0x0000FF);
}

ubuff8[0] = c3;
ubuff8[1] = c2;
ubuff8[2] = c1;
break;

case 4:
ubuff8 = (Uint8*) screen->pixels;
ubuff8 += (y*screen->pitch) + (x*4);
ubuff32 = (Uint32*)ubuff8;
*ubuff32 = color;
break;

default:
fprintf(stderr, "Error: Unknown bitdepth!\n");
}

graph_unlock();
}

// Some line drawing code

//----------------------------------------------------------

// A set of very useful macros that you will find in most
// code that I write whether I use them in a program or
// not.

#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define abs(a) (((a)<0) ? -(a) : (a))
#define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)

//----------------------------------------------------------

// The following code implements a Bresenham line drawing
// algorithm. There are 4 separate routines each optimized
// for one of the four pixel depths supported by SDL. SDL
// support many pixel formats, but it only support 8, 16,
// 24, and 32 bit pixels.

//----------------------------------------------------------

// Draw lines in 8 bit surfaces.

void line8(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) {
 int d;
 int x;
 int y;
 int ax;
 int ay;
 int sx;
 int sy;
 int dx;
 int dy;

 Uint8 *lineAddr;
 Sint32 yOffset;

 dx = x2 - x1;  
 ax = abs(dx) << 1;  
 sx = sign(dx);

 dy = y2 - y1;  
 ay = abs(dy) << 1;  
 sy = sign(dy);
 yOffset = sy * s->pitch;

 x = x1;
 y = y1;

 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
 if (ax>ay)
 {                      /* x dominant */
   d = ay - (ax >> 1);
   for (;;)
   {
     *(lineAddr + x) = (Uint8)color;

     if (x == x2)
     {
       return;
     }
     if (d>=0)
     {
       y += sy;
       lineAddr += yOffset;
       d -= ax;
     }
     x += sx;
     d += ay;
   }
 }
 else
 {                      /* y dominant */
   d = ax - (ay >> 1);
   for (;;)
   {
     *(lineAddr + x) = (Uint8)color;

     if (y == y2)
     {
       return;
     }
     if (d>=0) 
     {
       x += sx;
       d -= ay;
     }
     y += sy;
     lineAddr += yOffset;
     d += ax;
   }
 }
}

//----------------------------------------------------------

// Draw lines in 16 bit surfaces. Note that this code will
// also work on 15 bit surfaces.

void line16(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) {
 int d;
 int x;
 int y;
 int ax;
 int ay;
 int sx;
 int sy;
 int dx;
 int dy;

 Uint8 *lineAddr;
 Sint32 yOffset;

 dx = x2 - x1;  
 ax = abs(dx) << 1;  
 sx = sign(dx);

 dy = y2 - y1;  
 ay = abs(dy) << 1;  
 sy = sign(dy);
 yOffset = sy * s->pitch;

 x = x1;
 y = y1;

 lineAddr = ((Uint8 *)s->pixels) + (y * s->pitch);
 if (ax>ay)
 {                      /* x dominant */
   d = ay - (ax >> 1);
   for (;;)
   {
     *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;

     if (x == x2)
     {
       return;
     }
     if (d>=0)
     {
       y += sy;
       lineAddr += yOffset;
       d -= ax;
     }
     x += sx;
     d += ay;
   }
 }
 else
 {                      /* y dominant */
   d = ax - (ay >> 1);
   for (;;)
   {
     *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;

     if (y == y2)
     {
       return;
     }
     if (d>=0) 
     {
       x += sx;
       d -= ay;
     }
     y += sy;
     lineAddr += yOffset;
     d += ax;
   }
 }
}

//----------------------------------------------------------

// Draw lines in 24 bit surfaces. 24 bit surfaces require
// special handling because the pixels don't fall on even
// address boundaries. Instead of being able to store a
// single byte, word, or long you have to store 3
// individual bytes. As a result 24 bit graphics is slower
// than the other pixel sizes.

void line24(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) {
 int d;
 int x;
 int y;
 int ax;
 int ay;
 int sx;
 int sy;
 int dx;
 int dy;

 Uint8 *lineAddr;
 Sint32 yOffset;

#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
 color <<= 8;
#endif

 dx = x2 - x1;  
 ax = abs(dx) << 1;  
 sx = sign(dx);

 dy = y2 - y1;  
 ay = abs(dy) << 1;  
 sy = sign(dy);
 yOffset = sy * s->pitch;

 x = x1;
 y = y1;

 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
 if (ax>ay)
 {                      /* x dominant */
   d = ay - (ax >> 1);
   for (;;)
   {
     Uint8 *p = (lineAddr + (x * 3));
     memcpy(p, &color, 3);

     if (x == x2)
     {
       return;
     }
     if (d>=0)
     {
       y += sy;
       lineAddr += yOffset;
       d -= ax;
     }
     x += sx;
     d += ay;
   }
 }
 else
 {                      /* y dominant */
   d = ax - (ay >> 1);
   for (;;)
   {
     Uint8 *p = (lineAddr + (x * 3));
     memcpy(p, &color, 3);

     if (y == y2)
     {
       return;
     }
     if (d>=0) 
     {
       x += sx;
       d -= ay;
     }
     y += sy;
     lineAddr += yOffset;
     d += ax;
   }
 }
}

//----------------------------------------------------------

// Draw lines in 32 bit surfaces. Note that this routine
// ignores alpha values. It writes them into the surface
// if they are included in the pixel, but does nothing
// else with them.

void line32(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) {
 int d;
 int x;
 int y;
 int ax;
 int ay;
 int sx;
 int sy;
 int dx;
 int dy;

 Uint8 *lineAddr;
 Sint32 yOffset;

 dx = x2 - x1;  
 ax = abs(dx) << 1;  
 sx = sign(dx);

 dy = y2 - y1;  
 ay = abs(dy) << 1;  
 sy = sign(dy);
 yOffset = sy * s->pitch;

 x = x1;
 y = y1;

 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
 if (ax>ay)
 {                      /* x dominant */
   d = ay - (ax >> 1);
   for (;;)
   {
     *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;

     if (x == x2)
     {
       return;
     }
     if (d>=0)
     {
       y += sy;
       lineAddr += yOffset;
       d -= ax;
     }
     x += sx;
     d += ay;
   }
 }
 else
 {                      /* y dominant */
   d = ax - (ay >> 1);
   for (;;)
   {
     *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;

     if (y == y2)
     {
       return;
     }
     if (d>=0) 
     {
       x += sx;
       d -= ay;
     }
     y += sy;
     lineAddr += yOffset;
     d += ax;
   }
 }
}

//----------------------------------------------------------

// Examine the depth of a surface and select a line
// drawing routine optimized for the bytes/pixel of the
// surface.

void line(SDL_Surface *s, int x1, int y1, int x2, int y2, Uint32 color) {
 switch (s->format->BytesPerPixel)
 {
 case 1:
   line8(s, x1, y1, x2, y2, color);
   break;
 case 2:
   line16(s, x1, y1, x2, y2, color);
   break;
 case 3:
   line24(s, x1, y1, x2, y2, color);
   break;
 case 4:
   line32(s, x1, y1, x2, y2, color);
   break;
 }
}

 

Ja det var mye.

Lenke til kommentar

Whohooo! Da var "grunnpakka" for input via kommandolinje valg :)

 

int i = 0;

if (argc == 1) {
grav_displayhelp ();
}

printf ("argc: %d\n\n", argc);
for (i = 0; i < argc; ++i) {
printf ("Argument number: %d\n String: \"%s\"\n \n", i, argv[i]);
if (! strcmp(argv[i], "--help")) {
 grav_displayhelp ();
}
}

Lenke til kommentar

Og da gjør den jammen noe fornuftig også :)

//Check for arguments
int drawmethod = 0;	//Configurational variable, is used to switch betwen different graphics engines
int i = 0;  //Used to iterate through the arguments

if (argc == 1) {
grav_displayhelp ();
}

//check for arguments
for (i = 1; i < argc; ++i) {
//printf ("Argument number: %d\n String: \"%s\"\n \n", i, argv[i]); //Used while debugging
if (! strcmp(argv[i], "--help")) {
 	grav_displayhelp ();
 }
else if (! strcmp(argv[i], "--drawmethod")) {
 	i = i + 1;	//Jup, its a hack. But a beutifull one :) The next line is to check for errors introduced by it :)
 	if (i >= argc) {
   printf ("You must specify a drawmethod\n");
   grav_displayhelp ();
 	}
 	drawmethod = atoi(argv[i]);
 	//some error checking
 	if ((drawmethod != 1) && (drawmethod != 2)) {
   printf("%d is not a valid drawmethod\n", drawmethod);
   grav_displayhelp ();
 	}
 	
 	
 }
else {
 	printf ("Not a valid command line option. Use:\n");
 	grav_displayhelp ();
}
}

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...