Author Topic: Starfield using TinyPTC  (Read 627 times)

0 Members and 1 Guest are viewing this topic.

Offline TinDragon

  • Pentium
  • *****
  • Posts: 622
  • Karma: 23
    • View Profile
    • J2K's blog
Starfield using TinyPTC
« on: July 20, 2006 »


I made a simple starfield example in C using Tinyptc. It's maybe not the most impressive example but it shows how simple pixel drawing can be done and should hopefully be fairly easy to follow. If you downloaded either of the DevCPP Tinyptc setups I post you can replace test.c with this code and recompile to see it in action.  Anyone who has VC++, the code should also work if you have Tinyptc setup, I have tested in VC++.net 2003.

Code: [Select]
//
// TinyPTC Starfield
// Joncom2000 of TTD
//
// A little example of plotting some stars
#include "tinyptc.h"
#include <stdlib.h>
// Setuo screen
#define WIDTH 640
#define HEIGHT 480
#define SIZE WIDTH*HEIGHT
// Setup a buffer for our reading and writing   
static int buffer[SIZE];
// Function declares
void Cls(int rgb);
void WritePixel(int x,int y,int rgb);
// Starfield variables
int starx[600];
int stary[600];
int main()
{
int i; //For loops
int speed; //For speed calcs
// Open our window/screen
    if (!ptc_open("Stars_TTD by Joncom2000",WIDTH,HEIGHT)) return 1;
// setup stars
for (i=0; i<600; i++)
{
starx[i]=rand() % WIDTH;
stary[i]=rand() % HEIGHT;
}
// main loop exit
    while (1)
    {
Cls(0);
for (i=0; i<600; i++)
{
speed=1+(i % 3);
starx[i]=starx[i]+speed;
if (starx[i]>=WIDTH)
{
starx[i]=0;
}
WritePixel(starx[i],stary[i],((85*speed<<16)|(85*speed<<8)|85*speed));
}
        ptc_update(buffer);
    }
}

//-----------------------------------------------------------------
// Our Functions
//-----------------------------------------------------------------
void Cls(int rgb)
{
int x;
int y;
//Do cls stuff
for (y=0; y<HEIGHT; y++)
{
for (x=0; x<WIDTH; x++)
{
            buffer[(y*WIDTH+x)] =rgb;
}
}
}

void WritePixel(int x,int y,int rgb)
{
//Do write pixel
if(x>-1)
{
if(x<WIDTH)
{
if(y>-1)
{
if(y<HEIGHT)
{
buffer[(y*WIDTH+x)] =rgb;
}
}
}
}
}


Any questions, problems please post and I will try to help.

Cheers
Jon

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 16784
  • Karma: 439
  • evil/good
    • View Profile
    • My Homepage
Re: Starfield using TinyPTC
« Reply #1 on: July 20, 2006 »
Nice one Jon, thanks for the code, it's going to be useful to lots of people, not least of all myself.
 :cheers:
Shockwave ^ Codigos
Challenge Trophies Won:

Offline TinDragon

  • Pentium
  • *****
  • Posts: 622
  • Karma: 23
    • View Profile
    • J2K's blog
Re: Starfield using TinyPTC
« Reply #2 on: July 21, 2006 »
No problem Shockwave, I will point out that this is standard C code and does not use any of the more advanced C++ features. But since the TinyPTC lib is coded in C I thought it would make sense to start there for people trying to learn and use it :)