Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _UTILS_H
- #define _UTILS_H
- #include <stdbool.h>
- #include <stdint.h>
- typedef uint8_t u8;
- typedef int8_t s8;
- typedef uint16_t u16;
- typedef int16_t s16;
- typedef uint32_t u32;
- typedef int32_t s32;
- typedef union r16u {
- u16 v;
- struct { u8 l,h; };
- } r16u;
- typedef union r16s {
- s16 v;
- struct { u8 l; s8 h; };
- } r16s;
- #define _drlb(p,s) ( ((unsigned char)(p&1))<<s )
- #define _drhb(p,s) ( ((unsigned char)((p>>1)&1))<<s )
- #define DEFSPRITE_ROW(p0,p1,p2,p3,p4,p5,p6,p7) \
- _drlb(p0,7)|_drlb(p1,6)|_drlb(p2,5)|_drlb(p3,4)|_drlb(p4,3)|_drlb(p5,2)|_drlb(p6,1)|_drlb(p7,0), \
- _drhb(p0,7)|_drhb(p1,6)|_drhb(p2,5)|_drhb(p3,4)|_drhb(p4,3)|_drhb(p5,2)|_drhb(p6,1)|_drhb(p7,0)
- #define DEFSPRITE(p00,p01,p02,p03,p04,p05,p06,p07,p08,p09,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25,p26,p27,p28,p29,p30,p31,p32,p33,p34,p35,p36,p37,p38,p39,p40,p41,p42,p43,p44,p45,p46,p47,p48,p49,p50,p51,p52,p53,p54,p55,p56,p57,p58,p59,p60,p61,p62,p63) \
- DEFSPRITE_ROW(p00,p01,p02,p03,p04,p05,p06,p07), \
- DEFSPRITE_ROW(p08,p09,p10,p11,p12,p13,p14,p15), \
- DEFSPRITE_ROW(p16,p17,p18,p19,p20,p21,p22,p23), \
- DEFSPRITE_ROW(p24,p25,p26,p27,p28,p29,p30,p31), \
- DEFSPRITE_ROW(p32,p33,p34,p35,p36,p37,p38,p39), \
- DEFSPRITE_ROW(p40,p41,p42,p43,p44,p45,p46,p47), \
- DEFSPRITE_ROW(p48,p49,p50,p51,p52,p53,p54,p55), \
- DEFSPRITE_ROW(p56,p57,p58,p59,p60,p61,p62,p63)
- /*
- Template:
- DEFSPRITE(
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0),
- */
- //audio register stuff
- #define _A_R_1_1_1_1_1_1_1_1(b7, b6, b5, b4, b3, b2, b1, b0) \
- ( (unsigned char)( (((b7)&1)<<7) | (((b6)&1)<<6) | (((b5)&1)<<5) | (((b4)&1)<<4) | (((b3)&1)<<3) | (((b2)&1)<<2) | (((b1)&1)<<1) | ((b0)&1)) )
- #define _A_R_1_3_1_3(b7, b6_4, b3, b2_0) \
- ( (unsigned char)( (((b7)&1)<<7) | (((b6_4)&7)<<4) | (((b3)&1)<<3) | ((b2_0)&7) ) )
- #define _A_R_2_6(b7_6, b5_0) \
- ( (unsigned char)( (((b7_6)&3)<<6) | (b5_0&63) ) )
- #define _A_R_4_1_3(b7_4, b3, b2_0) \
- ( (unsigned char)( (((b7_4)&15)<<4) | (((b3)&1)<<3) | ((b2_0)&7) ) )
- #define _A_R_1_1_3_3(b7, b6, b5_3, b2_0) \
- ( (unsigned char)( (((b7)&1)<<7) | (((b6)&1)<<6) | (((b5_3)&7)<<3) | ((b2_0)&7) ) )
- #define APU_CTL NR52_REG
- #define APU_CTL_OFF APU_CTL &= 0x7f
- #define APU_CTL_ON APU_CTL |= 0x80
- #define APU_CTL_CH4_ISON ((APU_CTL&8) != 0)
- #define APU_CTL_CH3_ISON ((APU_CTL&4) != 0)
- #define APU_CTL_CH2_ISON ((APU_CTL&2) != 0)
- #define APU_CTL_CH1_ISON ((APU_CTL&1) != 0)
- #define APU_PAN NR51_REG
- #define APU_PAN_ASN(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r) \
- APU_PAN = _A_R_1_1_1_1_1_1_1_1(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r)
- #define APU_PAN_SET(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r) \
- APU_PAN |= _A_R_1_1_1_1_1_1_1_1(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r)
- #define APU_PAN_CLR(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r) \
- APU_PAN &= ~_A_R_1_1_1_1_1_1_1_1(ch4l, ch3l, ch2l, ch1l, ch4r, ch3r, ch2r, ch1r)
- #define APU_VOL NR50_REG
- #define APU_VOL_ASN(left_3, right_3, VIN_left_1, VIN_right_1) \
- APU_VOL = _A_R_1_3_1_3(VIN_left_1, left_3, VIN_right_1, right_3)
- #define APU_VOL_SET(left_3, right_3, VIN_left_1, VIN_right_1) \
- APU_VOL |= _A_R_1_3_1_3(VIN_left_1, left_3, VIN_right_1, right_3)
- #define APU_VOL_CLR(left_3, right_3, VIN_left_1, VIN_right_1) \
- APU_VOL &= ~_A_R_1_3_1_3(VIN_left_1, left_3, VIN_right_1, right_3)
- #define APU_CH1_SWEEP NR10_REG
- #define APU_CH1_SWEEP_ASN(pace_3, direction_1, individual_step_3) \
- APU_CH1_SWEEP = _A_R_1_3_1_3(0, pace_3, direction_1, individual_step_3)
- #define APU_CH1_SWEEP_SET(pace_3, direction_1, individual_step_3) \
- APU_CH1_SWEEP |= _A_R_1_3_1_3(0, pace_3, direction_1, individual_step_3)
- #define APU_CH1_SWEEP_CLR(pace_3, direction_1, individual_step_3) \
- APU_CH1_SWEEP &= ~_A_R_1_3_1_3(0, pace_3, direction_1, individual_step_3)
- #define APU_CH1_LENCYCLE NR11_REG
- #define APU_CH1_LENCYCLE_ASN(wave_duty_2, WO_initial_length_timer_6) \
- APU_CH1_LENCYCLE = _A_R_2_6(wave_duty_2, WO_initial_length_timer_6)
- #define APU_CH1_LENCYCLE_SET(wave_duty_2, WO_initial_length_timer_6) \
- APU_CH1_LENCYCLE |= _A_R_2_6(wave_duty_2, WO_initial_length_timer_6)
- #define APU_CH1_LENCYCLE_CLR(wave_duty_2, WO_initial_length_timer_6) \
- APU_CH1_LENCYCLE &= ~_A_R_2_6(wave_duty_2, WO_initial_length_timer_6)
- #define APU_CH1_VOLENV NR12_REG
- #define APU_CH1_VOLENV_ASN(initial_volume_4, env_dir_1, sweep_pace_3) \
- APU_CH1_VOLENV = _A_R_4_1_3(initial_volume_4, env_dir_1, sweep_pace_3)
- #define APU_CH1_VOLENV_SET(initial_volume_4, env_dir_1, sweep_pace_3) \
- APU_CH1_VOLENV |= _A_R_4_1_3(initial_volume_4, env_dir_1, sweep_pace_3)
- #define APU_CH1_VOLENV_CLR(initial_volume_4, env_dir_1, sweep_pace_3) \
- APU_CH1_VOLENV &= ~_A_R_4_1_3(initial_volume_4, env_dir_1, sweep_pace_3)
- #define APU_CH1_PERIODLO NR13_REG
- #define APU_CHR_PERIODLO_ASN(WO_period_low_8) \
- APU_CH1_PERIODLO = ((WO_period_low_8)&255)
- #define APU_CHR_PERIODLO_SET(WO_period_low_8) \
- APU_CH1_PERIODLO |= ((WO_period_low_8)&255)
- #define APU_CHR_PERIODLO_CLR(WO_period_low_8) \
- APU_CH1_PERIODLO &= ~((WO_period_low_8)&255)
- #define APU_CH1_PERIODHI NR14_REG
- #define APU_CH1_PERIODHI_ASN(WO_trigger_1, length_enable_1, WO_period_3) \
- APU_CH1_PERIODHI = _A_R_1_1_3_3(WO_trigger_1, length_enable_1, 0, WO_period_3)
- #define APU_CH1_PERIODHI_SET(WO_trigger_1, length_enable_1, WO_period_3) \
- APU_CH1_PERIODHI |= _A_R_1_1_3_3(WO_trigger_1, length_enable_1, 0, WO_period_3)
- #define APU_CH1_PERIODHI_CLR(WO_trigger_1, length_enable_1, WO_period_3) \
- APU_CH1_PERIODHI &= ~_A_R_1_1_3_3(WO_trigger_1, length_enable_1, 0, WO_period_3)
- #endif /* _UTILS_H */
- //(utils.h should be included before this)
- #ifndef _TILES_H
- #define _TILES_H
- #define B 3
- #define E 0
- #define _ 0
- const static uint8_t sprite_tiles[] = {
- DEFSPRITE( //0: run 0/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,_,
- _,B,B,_,_,B,B,_,
- _,B,B,_,_,_,_,_),
- DEFSPRITE( //1: run 1/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,B,
- B,B,B,_,_,_,B,B,
- B,B,_,_,_,_,_,_),
- DEFSPRITE( //2: run 2/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- B,B,B,B,B,B,B,B,
- B,B,_,_,_,_,B,B,
- _,_,_,_,_,_,B,B),
- DEFSPRITE( //3: run 3/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,_,
- _,B,B,_,_,B,B,_,
- _,_,_,_,_,B,B,_),
- DEFSPRITE( //4: run 4/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,_,
- _,_,B,B,B,B,_,_,
- _,_,_,_,B,B,_,_),
- DEFSPRITE( //5: run 5/5
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,_,
- _,_,B,B,B,B,_,_,
- _,_,B,B,_,_,_,_),
- DEFSPRITE( //6: foot forward (+y velocity)
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- B,B,B,B,B,B,B,_,
- B,B,_,_,_,B,B,B,
- _,_,_,_,_,_,B,B),
- DEFSPRITE( //7: idle
- _,B,B,B,B,B,B,_,
- B,B,E,B,B,B,E,B,
- B,B,E,B,B,B,E,B,
- B,B,B,B,B,B,B,B,
- B,B,B,E,E,B,B,B,
- _,B,B,B,B,B,B,_,
- _,B,B,_,_,B,B,_,
- _,B,B,_,_,B,B,_),
- };
- #undef E
- #undef B
- #undef _
- const static uint8_t background_tiles[] = {
- DEFSPRITE( //1
- 3,3,3,3,3,3,3,3,
- 1,0,0,0,0,0,1,3,
- 1,1,0,0,0,1,1,3,
- 1,1,1,0,1,1,1,3,
- 1,1,1,2,1,1,1,3,
- 1,1,2,2,2,1,1,3,
- 1,2,2,2,2,2,1,3,
- 2,2,2,2,2,2,2,3),
- };
- #define _ 3
- #define T 1
- const static uint8_t digit_tiles[] = {
- #ifndef TOP_LEFT_DIGIT_BIAS
- DEFSPRITE( //0
- _,_,_,_,_,_,_,_,
- _,_,T,T,T,T,T,_,
- _,T,T,_,_,_,T,T,
- _,T,T,_,_,T,T,T,
- _,T,T,_,T,T,T,T,
- _,T,T,T,T,_,T,T,
- _,T,T,T,_,_,T,T,
- _,_,T,T,T,T,T,_),
- DEFSPRITE( //1
- _,_,_,_,_,_,_,_,
- _,_,_,T,T,_,_,_,
- _,_,T,T,T,_,_,_,
- _,_,_,T,T,_,_,_,
- _,_,_,T,T,_,_,_,
- _,_,_,T,T,_,_,_,
- _,_,_,T,T,_,_,_,
- _,T,T,T,T,T,T,_),
- DEFSPRITE( //2
- _,_,_,_,_,_,_,_,
- _,_,T,T,T,T,_,_,
- _,T,T,_,_,T,T,_,
- _,_,_,_,_,T,T,_,
- _,_,_,T,T,T,_,_,
- _,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_,
- _,_,_,_,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,T,T,T,T,_,_,_,
- _,_,_,_,_,_,_,_),
- DEFSPRITE( //6
- _,_,T,T,T,_,_,_,
- _,T,T,_,_,_,_,_,
- T,T,_,_,_,_,_,_,
- T,T,T,T,T,_,_,_,
- T,T,_,_,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,T,T,T,T,_,_,_,
- _,_,_,_,_,_,_,_),
- DEFSPRITE( //7
- T,T,T,T,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,_,_,_,T,T,_,_,
- _,_,_,T,T,_,_,_,
- _,_,T,T,_,_,_,_,
- _,_,T,T,_,_,_,_,
- _,_,T,T,_,_,_,_,
- _,_,_,_,_,_,_,_),
- DEFSPRITE( //8
- _,T,T,T,T,_,_,_,
- T,T,_,_,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,T,T,T,T,_,_,_,
- T,T,_,_,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,T,T,T,T,_,_,_,
- _,_,_,_,_,_,_,_),
- DEFSPRITE( //9
- _,T,T,T,T,_,_,_,
- T,T,_,_,T,T,_,_,
- T,T,_,_,T,T,_,_,
- _,T,T,T,T,T,_,_,
- _,_,_,_,T,T,_,_,
- _,_,_,T,T,_,_,_,
- _,T,T,T,_,_,_,_,
- _,_,_,_,_,_,_,_),
- #endif /* TOP_LEFT_DIGIT_BIAS */
- };
- #undef _
- #undef T
- #endif /* _TILES_H */
- #include <gb/gb.h>
- #include <gb/bcd.h>
- #include <rand.h>
- //#include <stdio.h>
- //#include <gbdk/emu_debug.h>
- //#include "hUGEDriver.h"
- #include "utils.h"
- #include "tiles.h"
- #define imm_mod(n, n_lvalue, m_ge) if((n)>=(m_ge)){ (n_lvalue) -= (m_ge); }
- typedef struct player_s {
- r16u x, y;
- r16s vx, vy;
- r16u run;
- bool on_ground;
- u8 _;
- } player_s;
- static player_s player;
- //(does not update the physical position of the player's sprite)
- void move_player(void/*tbd: s16 dx, s16 dy*/){ //__critical, maybe?
- //tbd, whatever
- /*
- if(dx){
- set_sprite_prop(0, (dx<0) ? S_FLIPX : 0);
- //
- if(dx < 0) dx = -dx;
- player.run.v += dx;
- player.run.h %= 6;
- } else {
- set_sprite_tile(0, 16|7);
- }
- if(dy){
- } else {
- }
- if(player.on_ground){
- }
- */
- }
- static inline u8 count_bits_8(u8 n){
- u8 count = 0;
- while(n){
- count += n&1;
- n >>= 1;
- }
- return count;
- }
- static u16 score;
- typedef union bcd {
- BCD v;
- struct { u8 a,b,c,d; };
- } bcd;
- #define blit_score() _blit_u16(128, 152, score)
- #define blit_u16(n) _blit_u16(128, 152, (u16)(n))
- void _blit_u16(u8 x, u8 y, u16 n){
- static bcd score_bcd;
- //sprites 1-5 are the digits
- move_sprite(1, x, y); x+=8;
- move_sprite(2, x, y); x+=8;
- move_sprite(3, x, y); x+=8;
- move_sprite(4, x, y); x+=8;
- move_sprite(5, x, y);
- //only update tile values if y > 0
- if(y > 0){
- score_bcd.v = 0;
- uint2bcd(n, &score_bcd.v);
- #define lo_digit(_byte) ((_byte)&15)
- #define hi_digit(_byte) (((_byte)>>4)&15)
- set_sprite_tile(1, lo_digit(score_bcd.c));
- set_sprite_tile(2, hi_digit(score_bcd.b));
- set_sprite_tile(3, lo_digit(score_bcd.b));
- set_sprite_tile(4, hi_digit(score_bcd.a));
- set_sprite_tile(5, lo_digit(score_bcd.a));
- }
- }
- static u8 clear_buffer[32];
- void bkg_clear(u8 value){
- for(u8 t=0; t<32; ++t)
- clear_buffer[t] = value;
- for(u8 y=0; y<32; ++y)
- set_bkg_tiles(0, y, 32,1, clear_buffer);
- }
- static u8 bkg_row[20];
- static inline void stamp_row(u8 y){
- set_bkg_tiles(0, y&31, 20, 1, bkg_row);
- }
- void generate_row(bool filled){
- static r16u new_row;
- //copy of new_row.l; used for bitwise stuff
- static u8 tmp_row;
- //solid floor at start
- static u16 old_row = 0b111111111;
- //a row's edges are always supposed to be set no matter what
- bkg_row[ 0] = 1;
- bkg_row[19] = 1;
- if(!filled){
- for(char i=1; i<19; ++i) bkg_row[i] = 0;
- return;
- }
- //_generate_new_platform:
- new_row.v = randw();
- //put something in row if the call to randw
- //either turned up empty or completely full
- if(!new_row.v || (new_row.v&0b111111111)==0b111111111)
- new_row.v = 0b101010101;
- else
- new_row.v &= 0b111111111;
- //if there are no platforms under any of the new platform's
- //holes, then attempt to generate another row
- if((new_row.v | old_row) == new_row.v){
- new_row.v = ~old_row;
- new_row.v &= 0b111111111;
- //goto _generate_new_platform;
- }
- //turn the first 9 bits of new_row into tile data,
- //where 1 bit = 2 set tiles in a given row,
- //for a total of 18 (excluding edges, which are always set)
- tmp_row = new_row.l;
- bkg_row[ 1] = tmp_row&1; tmp_row >>= 1;
- bkg_row[ 3] = tmp_row&1; tmp_row >>= 1;
- bkg_row[ 5] = tmp_row&1; tmp_row >>= 1;
- bkg_row[ 7] = tmp_row&1; tmp_row >>= 1;
- bkg_row[ 9] = tmp_row&1; tmp_row >>= 1;
- bkg_row[11] = tmp_row&1; tmp_row >>= 1;
- bkg_row[13] = tmp_row&1; tmp_row >>= 1;
- bkg_row[15] = tmp_row&1; //no need to shift here
- bkg_row[17] = new_row.h&1; //only the low bit of .h is used
- //copy the other half of the row's sections
- bkg_row[ 2] = bkg_row[ 1];
- bkg_row[ 4] = bkg_row[ 3];
- bkg_row[ 6] = bkg_row[ 5];
- bkg_row[ 8] = bkg_row[ 7];
- bkg_row[10] = bkg_row[ 9];
- bkg_row[12] = bkg_row[11];
- bkg_row[14] = bkg_row[13];
- bkg_row[16] = bkg_row[15];
- bkg_row[18] = bkg_row[17];
- //because only tmp_row has actually been clobbered,
- //it should be safe to update the value like this
- old_row = new_row.v&0b111111111;
- }
- static r16u vscroll_pos; //global y scroll position; .v -= to advance downwards
- static r16u vscroll_change; //used to keep track of when to generate new
- static u8 vscroll_which;
- static u8 vscroll_tile = 253; // should start at 0 - 3
- static u16 vscroll_speed = 0x40;
- //scrolls down vertically, potentially generating new rows
- void vscroll(u16 delta){
- vscroll_pos.v -= delta;
- vscroll_change.v += delta;
- player.y.v += delta;
- //generate new row(s)
- if(vscroll_change.h >= 8){
- //any change greater than 31 is pointless,
- //since the background is only 32 tiles big
- //so, the change is &='d with 0x1fff, which is
- //about 31.996 when interpreted as an unsigned
- //fixed-point number with an 8-bit fraction
- //(hopefully this works as it should lol)
- vscroll_change.v &= 0x1fff;
- static bool filled_row;
- do {
- filled_row = !(++vscroll_which&3);
- if(filled_row){
- ++score;
- }
- __critical {
- generate_row(filled_row);
- stamp_row(vscroll_tile);
- }
- --vscroll_tile;
- } while((vscroll_change.h-=8) >= 8);
- }
- //only use high byte of position, (low byte denotes subpixels)
- SCY_REG = vscroll_pos.h;
- }
- void main(void){
- //set tile data
- set_bkg_data(1, 1, background_tiles);
- set_sprite_data( 0, 10, digit_tiles);
- set_sprite_data(16, 8, sprite_tiles);
- set_sprite_tile(0, 16|7); //player at idle
- //generate start area
- bkg_clear(1);
- generate_row(true);
- stamp_row(30);
- generate_row(false);
- stamp_row(31);
- stamp_row( 0);
- stamp_row( 1);
- //0.5 pixels per frame to start
- vscroll_speed = 1 * 128;
- //put player at start
- player.x.v = 84 * 256;
- player.y.v = 24 * 256;
- move_sprite(0, player.x.h, player.y.h);
- SHOW_BKG;
- SHOW_SPRITES;
- //seed rng
- static u8 padstate = 0;
- while(!joypad()) vsync(); //wait for press
- while( joypad()){ vsync(); padstate|=joypad(); }//wait for unpress
- padstate = count_bits_8(padstate);
- if(padstate > 4) padstate = 4;
- u16 seed = sys_time<<padstate;
- if(!seed) seed = sys_time;
- initrand(seed);
- APU_CTL_ON;
- APU_PAN_ASN(1,1,1,1, 1,1,1,1);
- APU_VOL_ASN(3,3, 0,0);
- __critical {
- //hUGE_init(&song_test);
- //add_VBL(hUGE_dosound);
- }
- while(1){
- padstate = joypad();
- if(!padstate) vscroll(vscroll_speed);
- blit_u16(score);
- if(!(sys_time&63))
- ++vscroll_speed;
- move_sprite(0, player.x.h, player.y.h+1 );
- vsync();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement