Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created November 13, 2011 18:05
Show Gist options
  • Select an option

  • Save Gi133/1362413 to your computer and use it in GitHub Desktop.

Select an option

Save Gi133/1362413 to your computer and use it in GitHub Desktop.
Lab 4 for Computer and Graphics Architecture
//================
// HEADERS
//================
#include <stdint.h>
#include "gba.h"
#include <stdlib.h>
#include <time.h>
//================
// CONSTANTS
//================
#define BG2_ENABLE 0x400
#define MODE3 0x3
#define FRONTBUFFER (u16*)0x6000000
#define RGB(r,g,b) ((b<<10)+(g<<5)+r) //u16 --> unsigned short, this is macro for RGB() 15bit color format
#define ScreenWidth 240
#define ScreenHeight 160
//================
// GLOBALS
//================
u16* g_uspVideoBuffer = FRONTBUFFER; //This is were we are currently drawing
//================
// FUNCTIONS
//================
//================
// Function: PlotPixel16(int, int, unsigned short int)
// Notes: Plots a pixel in the specified colour
// Date: 14/2/2002
//================
void PlotPixel16(int in_iX,int in_iY, unsigned short int in_usColour)
{
g_uspVideoBuffer[(in_iY) * ScreenWidth + (in_iX)] = (in_usColour);
}
//================
// Function: main()
// Notes: Main entry point into rom
// Date: 14/2/2002
//================
int main()
{
//REG_DISPCNT (defined in gba.h) is the main display register that controls
//the mode of operation which backgrounds are enabled and which buffer is
//currently being drawn by the GPU.
//this sets the screen mode to mode three and enables background 2
//background 2 is required for all bitmap modes(3-5)
REG_DISPCNT = MODE3 | BG2_ENABLE;
srand(time(NULL));
unsigned short red = RGB(31,0,0);
unsigned short green= RGB(0,31,0);
unsigned short blue = RGB(0,0,31);
unsigned short white= RGB(31,31,31);
unsigned short black= RGB(0,0,0);
// Assign new variables to make colors move around
int old_xred, old_xgreen, old_xblue, old_xwhite, old_yred, old_ygreen, old_yblue, old_ywhite;
int xred=110, xgreen=130, xblue=110, xwhite=130, yred=70, ygreen=70, yblue=90, ywhite=90;
//Draw Original and New Pixels.
PlotPixel16(xred,yred,red);
PlotPixel16(xgreen,ygreen,green);
PlotPixel16(xblue,yblue,blue);
PlotPixel16(xwhite,ywhite,white);
while(1){
//Save old coordinates
old_xred=xred;
old_xgreen=xgreen;
old_xblue=xblue;
old_xwhite=xwhite;
old_yred=yred;
old_ygreen=ygreen;
old_yblue=yblue;
old_ywhite=ywhite;
//Erase old pixels
PlotPixel16(old_xred,old_yred,black);
PlotPixel16(old_xgreen,old_ygreen,black);
PlotPixel16(old_xblue,old_yblue,black);
PlotPixel16(old_xwhite,old_ywhite,black);
//Come up with new coordinates for the new pixels
xred=rand()%ScreenWidth;
xgreen=rand()%ScreenWidth;
xblue=rand()%ScreenWidth;
xwhite=rand()%ScreenWidth;
yred=rand()%ScreenHeight;
ygreen=rand()%ScreenHeight;
yblue=rand()%ScreenHeight;
ywhite=rand()%ScreenHeight;
//Draw new pixels
for (int i=0; i<=350; i=i+1) {
PlotPixel16(xred,yred,red);
PlotPixel16(xgreen,ygreen,green);
PlotPixel16(xblue,yblue,blue);
PlotPixel16(xwhite,ywhite,white);
}
//No WaitVSync in this lab's gba.h apparently so delay is a bit more crude for now.
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment