www.xbdev.net
xbdev - software development
Friday April 19, 2024
home | contact | Support | XBOX Programming More than just a hobby...
>>
     
 

XBOX Programming

More than just a hobby...

 

SetPixels on XBOX... and draw a picture


So you have an xbox, and well you don't want to use the xdk so how do you go about programming software for it?  Well at the end of the day all the xbox is, is a PC with a few minor differences.

This simple, very simple tutorial will show you the way to making software for the xbox using your own little compiler.  But to start with there are a few things you should learn, these are how we can access the video card and how it works.

If you go back, way back to the days of DOS.... many cencuries ago..lol... well if you wanted to draw graphics to the screen you would use the same principle where going to use here, which is direct memory access.  As memory from 0xf0040000 to 0xf0086000 (640x480=307200 or 46000 hex) is an array of Blue Green and Red pixel values (8 bits each).

In the days of dos it was 0xa0000 which was used for memory access... but its all changed since then. (due to things like PIC busses, and higher memory and resolutions etc).

So keeping to the simple things, lets say we start as follows:

void main()

{

//Body

};

This should be all to simple now, this is our entry point... our point of entry for our program.  I hope I haven't lost you already :)

So how would we get this memory address?  In the world of c anc c++ we use pointers which refer to memory locations, and so all we would have to do to get to the location in memory is:

unsigned int VIDEO_MEMORY 0xf0040000;

((unsigned char*)VIDEO_MEMORY)

It may look a bit cryptic, and I'm not sure if you can see what is happening above, espcially for those who are new to c and c++ and haven't quiet got the grasp of pointers.  But what is mean is (unsigned char*) is a pointer to a memory location, and then we are going to set it to VIDEO_MEMORY which has the value of 0xf0040000.  This would then allow us to set the first pixel of this memory to 1 if we did this ((unsigned char*)VIDEO_MEMORY)0x01;

So where next?

Well I guess your not very impressed with what I've shown you up to now... and it isn't that exciting until you see it in action.. but be patient.

One things to not as well, as I seem to forget to mention this, the pixels are stored as blue-green-red, and not red-green-blue as you'd expect.

So on with the story, now to make things simple in the beginning and quench your thirst I set about making a simple set pixel colour function.  This would allow us to set colours on the screen relatively easily.

Now I wanted to give you an easy ride, so I decided to make a very simple function called "setpixel(..)" where you could call it with a red-green-blue value and position on the screen and it would plot that colour.  At the time of writing this, it was getting closer to halloween so the image I used as an example will explain it all.... if you want to know what it is...you'll have to run the software :)

#define SCREEN_WIDTH 640

#define SCREEN_HEIGHT 480

// Point in memory where we should start writing to.

unsigned int framebuffer = 0xf0040000;

void setpixel(int x, int y, unsigned int pixelrgb)

{

// Here we convert a rgb to a bgr ordering of the bits.

unsigned int pixelbgr = (pixelrgb & 0xff00ff00) | ((pixelrgb & 0x00ff0000) >> 16) | ((pixelrgb & 0x000000ff) << 16);

// This line helps us make the picture we are drawing look better for this

// example so that you start to drewl..lol. Basically it only draws pixels to

// the screen which are not 0x0..0

if(pixelrgb != 0x000000)

{

unsigned char weight = 255/3;

for (unsigned int i=0; i<3; i++)

{

unsigned int p1 = ((pixelbgr >> (8*i) ) & 255);// * weight/255;

((unsigned char*)framebuffer)[(y*SCREEN_WIDTH+x)*4+i] = p1;

}

}

}

Well there you go, its nice and tidy, all you have to do now is call setpixel and draw pixels on the screen... of course please please remember this is a simple tutorial, I have not included any clipping or checking of values passed.

So to show what you could actually do with such a piece of code, I wrote a small program which draws an image to screen... the image of course put in a pic.h header file as would luck like:

// Declared in pic.h

unsigned int imageWidth = 100;

unsigned int imageHeight = 116;

unsigned int imageBits[] =

{

0xfdfdfd, 0x000000, 0x000000, 0x000000, 0x000000, .........,

0x000000, ........

...........

.....

etc etc

0x000000, 0x000000, 0x000000, 0x000000, 0x000000, .........,

};

Where each bit is a red, green blue value... so 0x000000 is black, and 0xffffff is white etc.

Then for the whole of the main program is as follows:

/***************************************************************************/

/*                                                                                                                                                                          */

/* Simple set pixel - how to?                                                                                                                             */

/*                                                                                                                                                                          */

/***************************************************************************/

// This file just holds and array of colours in the form of red,green,blue.

#include "pic.h"

 

#define SCREEN_WIDTH 640

#define SCREEN_HEIGHT 480

// Point in memory where we should start writing to.

unsigned int framebuffer = 0xf0040000;

void setpixel(int x, int y, unsigned int pixelrgb)

{

// Here we convert a rgb to a bgr ordering of the bits.

unsigned int pixelbgr = (pixelrgb & 0xff00ff00) | ((pixelrgb & 0x00ff0000) >> 16) | ((pixelrgb & 0x000000ff) << 16);

// This line helps us make the picture we are drawing look better for this

// example so that you start to drewl..lol. Basically it only draws pixels to

// the screen which are not 0x0..0

if(pixelrgb != 0x000000)

{

unsigned char weight = 255/3;

for (unsigned int i=0; i<3; i++)

{

unsigned int p1 = ((pixelbgr >> (8*i) ) & 255);// * weight/255;

((unsigned char*)framebuffer)[(y*SCREEN_WIDTH+x)*4+i] = p1;

}

}

}

 

// Simple entry point.

void main()

{

while(true)

{

for(unsigned int y=0; y<imageHeight; y++)

{

for(unsigned int x=0; x<imageWidth; x++)

{

// Get the rgb value from our bits array in pic.h

unsigned int pixelrgb = imageBits[y*(imageWidth-1)+x];

setpixel(x -100, y+50, pixelrgb);

}

}

}

}

Well because I'm such a nice guy I made it possible for you to download a piece of code and have a go... I promise you... when you run this baby up and see what you get on the screen you won't be dissapointed.

Download

Note you can create images the same way I have done here by exporting images to the xbm image format.

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.