Starfield code

Starfield code

Postby SG57 » Sat Jun 30, 2012 1:30 am

Hey fellow hackvisioners, sooo Im stuck waiting few more days for my FTDI breakout board to actually program my hackvision. However, this hasnt stopped me from writing some small demos/screensavers. So here is a starfield, it compiles fine in the arduino IDE so if anybody with a programmer can stick this on their chip and give it a run? Once i can program my own I'll be popping outa few games for fun ;]

**edit** working code in following post
Last edited by SG57 on Sat Jul 14, 2012 11:11 pm, edited 1 time in total.
SG57
Newbie
Newbie
 
Posts: 3
Joined: Thu Jun 28, 2012 10:10 pm

Re: Starfield code

Postby SG57 » Sat Jul 14, 2012 11:10 pm

OK finally got my $2 breakout board from China, debugged my horrible code and got the starfield working. I had a few oversights coming from platforms that had better hardware, such as bytes being unsigned, string literals being costly and structures add up when you have a lot of them. I tuned the constants just below artifacts began appearing. I suppose lowering the resolution would allow more stars onscreen but for now 16 still creates the effect.

In anycase, here is the working final code, its your typical Starfield/hyperspace view thing with stars wizzing by in pseudo-3D. you can adjust the Speed with up/down on the on-board controller.
Code: Select all
/** Starfield for Hackvision
  By Cord Rehn 2012 **/
 

#include <avr/pgmspace.h>
#include <TVout.h>
#include <video_gen.h>
#include <Controllers.h>
#include <fontALL.h>


#define TV_W 136
#define TV_H 98

TVout tv;

#define NUM_STARS 16
#define Z_MAX 6.f

byte SPEED;
char speed_string[11], s[4];

prog_char s0[] PROGMEM = "SPEED ";

PROGMEM const char *strings[] = {s0};
 
struct Star {
  int x, y;
  byte old_x, old_y;
  float z;
} Starfield[NUM_STARS];

void initStar(int index) {
  Starfield[index].x = random(TV_W*2) - TV_W;
  Starfield[index].y = random(TV_H*2) - TV_H;
  Starfield[index].old_x = Starfield[index].old_y = 255;
  Starfield[index].z = Z_MAX;
}

void setSpeed(byte speed) {
  tv.tone(speed < SPEED ? 800 : 1046, 20);
 
  SPEED = speed;
  speed_string[0] = s[0] = '\0';
  strcpy_P(speed_string, (char *)pgm_read_word(&(strings[0])));
  snprintf(s, 4, "%d  ", SPEED); // 2 spaces are important, clears the 2nd and 3rd digit spots for a 1 digit speed when printed
  strncat(speed_string, s, 11);
}

void setup()  {
  // If pin 12 is pulled LOW, then the PAL jumper is shorted.
  pinMode(12, INPUT);
  digitalWrite(12, HIGH);

  if (digitalRead(12) == LOW)
    tv.begin(_PAL, TV_W, TV_H);
  else
    tv.begin(_NTSC, TV_W, TV_H);

  tv.select_font(font4x6);
  randomSeed(analogRead(0));
 
  tv.delay(10);
 
  // init star field
  for (int i = 0; i < NUM_STARS; ++i) {
    initStar(i);
    Starfield[i].z = random(Z_MAX+1);
  }
 
  setSpeed(100);
}


void loop() {
  // input
  if (Controller.upPressed() && SPEED != 255)
    setSpeed(SPEED + 1);
  else if (Controller.downPressed() && SPEED != 0)
    setSpeed(SPEED - 1);
   
  // draw
  for (int i = 0; i < NUM_STARS; ++i) {
    if (Starfield[i].old_x != 255)
      tv.set_pixel(Starfield[i].old_x, Starfield[i].old_y, 0);
   
    Starfield[i].z -= (float)SPEED/1000.f;
    if (Starfield[i].z < (float)SPEED/1000.f)
      initStar(i);
   
    byte x = TV_W / 2 + Starfield[i].x / Starfield[i].z,
         y = TV_H / 2 + Starfield[i].y / Starfield[i].z;
    if (x < 0 || y < 0 || x >= TV_W || y >= TV_H)
      initStar(i);
    else {
      tv.set_pixel(x, y, 1);
      Starfield[i].old_x = x;
      Starfield[i].old_y = y;
    }
  }
 
  tv.print(0, 0, speed_string);
}
SG57
Newbie
Newbie
 
Posts: 3
Joined: Thu Jun 28, 2012 10:10 pm

Re: Starfield code

Postby Michael » Sun Jul 15, 2012 8:01 am

Very nice! This looks great, and could be the basis for a new game. I have created a zip file for easy download and installation into the Arduino sketchbook folder. Thanks for this contribution!
Attachments
Starfield.zip
(1.25 KiB) Downloaded 88 times
Michael
Administrator
Administrator
 
Posts: 460
Joined: Sat May 29, 2010 8:24 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest