Shmup/src/starfield.cpp

156 lines
3.7 KiB
C++

#include "starfield.h"
#include <azur/azur.h>
#include <azur/gint/render.h>
#include <cstdint>
#include <stdlib.h>
#include <gint/rtc.h>
#include <num/num.h>
#include "MyAzurShaders.h"
Star::Star( void )
{
x = rand() % 396;
y = libnum::num( rand() % 224 );
size = 1 + ( rand() % 4 );
sy = libnum::num( size );
int colorrandom = rand() % 4;
color = 0xFFFF;
/*
if (colorrandom==0) color = 0xFFFF;
else if (colorrandom==1) color = 0xFFE0;
else if (colorrandom==2) color = 0xFB80;
else color = 0xF80D;
*/
}
Star::~Star()
{
}
void Star::Update( libnum::num dt )
{
//libnum::num a = libnum::num( dt / 12000.0f );
//y += sy * a;
y+= sy * dt;
if (y >= 224)
{
x = rand() % 396;
y = libnum::num( 0 );
}
}
Starfield::Starfield( )
{
srand(rtc_ticks());
for(int i=0; i<100; i++)
{
Star *s = new Star( );
MyStars.push_back( s );
}
}
Starfield::~Starfield( )
{
for(auto& s : MyStars)
delete(s);
for(auto& list : PixelListPerFragment)
{
for(auto& p : list)
delete(p);
list.clear();
}
MyStars.clear();
}
void Starfield::Update( float dt )
{
libnum::num a = libnum::num( dt / 50000.f );
for(auto& s : MyStars)
s->Update( a );
}
void Starfield::AddPixel( int x, int y, int c )
{
// check if the point is in the range screen
if(x >= azrp_width || x < 0 || y >= azrp_height || y < 0)
return;
uint8_t current_frag = y >> 4; // each fragment is 16pixel high : so fragment number for the current point is y/16 or y>>4
Pixel *MyPix = new Pixel( x, y & 15, c ); // consider the local offset of the point in the current fragment (y & 15)
PixelListPerFragment[ current_frag ].push_back( MyPix ); // add the pixel to the appropriate list
}
void Starfield::Render( void )
{
for(auto& list : PixelListPerFragment)
{
for(auto& p : list)
{
delete(p);
}
list.clear();
}
for(auto& s : MyStars)
{
//TODO :The only considered case is for a small star 1x1 pixel
//TODO :Other sizes to be added right after this case
if (s->size==1)
{
AddPixel( s->x, (int) s->y, s->color );
}
else if (s->size==2)
{
AddPixel( s->x, (int) s->y, s->color );
AddPixel( s->x+1, (int) s->y, s->color );
AddPixel( s->x, (int) s->y+1, s->color );
AddPixel( s->x+1, (int) s->y+1, s->color );
}
else if (s->size==3)
{
AddPixel( s->x+1, (int) s->y, s->color );
AddPixel( s->x-1, (int) s->y+1, s->color );
AddPixel( s->x, (int) s->y+1, s->color );
AddPixel( s->x+1, (int) s->y+1, s->color );
AddPixel( s->x+1, (int) s->y+2, s->color );
}
else if (s->size==4)
{
AddPixel( s->x-1, (int) s->y-1, s->color );
AddPixel( s->x-1, (int) s->y, s->color );
AddPixel( s->x-1, (int) s->y+1, s->color );
AddPixel( s->x, (int) s->y-1, s->color );
AddPixel( s->x, (int) s->y, s->color );
AddPixel( s->x, (int) s->y+1, s->color );
AddPixel( s->x+1, (int) s->y-1, s->color );
AddPixel( s->x+1, (int) s->y, s->color );
AddPixel( s->x+1, (int) s->y+1, s->color );
}
}
// call the PixelList shader with the appropriate lists
for(unsigned int i=0; i<14; i++)
//for(unsigned int i=0; i<PixelListPerFragment.size(); i++)
if (!PixelListPerFragment[i].empty())
azrp_pixellist( PixelListPerFragment[i], i );
}