From 5101d04e78d0f25a571ae80b3f948f02875ccb55 Mon Sep 17 00:00:00 2001 From: Mikolaj Feliks Date: Sun, 22 Oct 2023 11:47:40 +0200 Subject: [PATCH] Make code more portable --- bonus.c | 2 +- bullets.c | 4 ++-- control.c | 1 + enemies.c | 44 ++++++++++++++++++++++---------------- font.c | 3 +++ intro.c | 2 +- main.c | 4 +++- results.c | 3 ++- sound.c | 63 +------------------------------------------------------ stars.c | 6 +++--- 10 files changed, 43 insertions(+), 89 deletions(-) diff --git a/bonus.c b/bonus.c index d53d543..9dd7023 100644 --- a/bonus.c +++ b/bonus.c @@ -19,7 +19,7 @@ void b_nadaj0(STATEK* bon) void bonus_inicjuj(STATEK* bon) { int i, j = 36; - bon->x = random(299); + bon->x = rand() % 299; bon->y = 0; bon->licznik = 0; for (i = 0; i < 6; i++) diff --git a/bullets.c b/bullets.c index 7fa73c1..1048722 100644 --- a/bullets.c +++ b/bullets.c @@ -113,7 +113,7 @@ void p_nadaj0(void) void p_kontrola_kaps(POCISK* poc, KAPSULA* kaps, POLE* ple) { if (poc->x > kaps->x && poc->x < kaps->x + 19 && poc->y > kaps->y && poc->y < kaps->y + 19 && !kaps->trafiona && !ple->aktywne) { - play_sound(&soundtab[random(SND_EXPLOSION)]); + play_sound(&soundtab[rand() % SND_EXPLOSION]); kaps->giwera--; kaps->trafiona = 1; @@ -136,7 +136,7 @@ void p_kontrola_stat(POCISK* poc, STATEK* sta, KAPSULA* kaps) kaps->licznik++; ile_aktywnych--; - play_sound(&soundtab[random(SND_EXPLOSION)]); + play_sound(&soundtab[rand() % SND_EXPLOSION]); } } diff --git a/control.c b/control.c index 2dc20b6..df0e1e4 100644 --- a/control.c +++ b/control.c @@ -7,6 +7,7 @@ *****************************************/ #include +#include #include "globals.h" diff --git a/enemies.c b/enemies.c index d2b124f..73f1aab 100644 --- a/enemies.c +++ b/enemies.c @@ -13,36 +13,44 @@ void stat_inicjuj(STATEK* sta, KAPSULA* kaps) { int i, j; - if (!random(2)) - sta->x = random(30); - else - sta->x = random(30) + 269; - sta->y = random(79); + + if ((rand() % 2) == 0) { + sta->x = rand() % 30; + } + else { + sta->x = (rand() % 30) + 269; + } + sta->y = rand() % 79; + sta->zm_x = 0; sta->zm_y = 0; sta->aktywny = 1; - sta->xs_min = random(319); - sta->xs_max = sta->xs_min + random(319 - sta->xs_min); - for (i = 0; i < 3; i++) - sta->poc_x[i] = random(319); + + sta->xs_min = rand() % 319; + sta->xs_max = sta->xs_min + rand() % (319 - sta->xs_min); + + for (i = 0; i < 3; i++) { + sta->poc_x[i] = rand() % 319; + } + sta->licznik = 0; sta->k = 0; sta->laser_pom = 0; if (kaps->punkty < 20) sta->giwera = 0; else if (kaps->punkty >= 20 && kaps->punkty < 40) - sta->giwera = random(2); + sta->giwera = rand() % 2; else if (kaps->punkty >= 40 && kaps->punkty < 60) - sta->giwera = random(3); + sta->giwera = rand() % 3; else if (kaps->punkty >= 60 && kaps->punkty < 80) - sta->giwera = random(4); + sta->giwera = rand() % 4; else if (kaps->punkty >= 80 && kaps->punkty < 100) - sta->giwera = random(5); + sta->giwera = rand() % 5; else - sta->giwera = random(6); + sta->giwera = rand() % 6; if (sta->giwera != 5) { - sta->ruch = random(2); - sta->v = random(2); + sta->ruch = rand() % 2; + sta->v = rand() % 2; } else { sta->ruch = 2; sta->v = 1; @@ -209,8 +217,8 @@ void stat_ruch2(STATEK* statek) } } if (!statek->zm_x && !statek->zm_y) { - statek->kx = random(299); - statek->ky = random(100); + statek->kx = rand() % 299; + statek->ky = rand() % 100; statek->zm_x = 1; statek->zm_y = 1; } diff --git a/font.c b/font.c index 18a0c9c..b5c8234 100644 --- a/font.c +++ b/font.c @@ -6,10 +6,13 @@ (C) 2001, 2023 M. Feliks *****************************************/ +#include + #include "globals.h" unsigned char chardata[]; + void init_font(void) { } diff --git a/intro.c b/intro.c index ba159fe..66fa9f4 100644 --- a/intro.c +++ b/intro.c @@ -93,7 +93,7 @@ void intro(void) timer_wait(); } - play_sound(&soundtab[random(SND_EXPLOSION)]); + play_sound(&soundtab[rand() % SND_EXPLOSION]); flash(); while (1) { diff --git a/main.c b/main.c index cf6f0de..a0e9d92 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "globals.h" @@ -55,7 +56,8 @@ void main(void) paleta_inicjuj(); save_pal(); w_inicjuj_sincos(); - randomize(); + + srand(time(NULL)); sb_dsp_out(ON_SPEAKER); enable_dma_int(); diff --git a/results.c b/results.c index 3181ec0..29e9a5c 100644 --- a/results.c +++ b/results.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "globals.h" @@ -147,7 +148,7 @@ void analiza_wynikow(int punkty) zmien_rekordy(punkty); drukuj_liste(); - play_sound(&soundtab[random(SND_EXPLOSION)]); + play_sound(&soundtab[rand() % SND_EXPLOSION]); flash(); while (1) { diff --git a/sound.c b/sound.c index dc375aa..1ab2081 100644 --- a/sound.c +++ b/sound.c @@ -6,31 +6,8 @@ (C) 2001, 2023 M. Feliks *****************************************/ -#include -#include -#include - #include "globals.h" -#define SB_BASE_ADDRESS 0x220 -#define DSP_OUT 0x10 - -#define DMA_CHANNEL 0x01 -#define DMA_PAGE_PORT 0x83 -#define DMA_OFFSET_PORT 0x02 -#define DMA_LENGHT_PORT 0x03 -#define DMA_MODE 0x48 -#define DMA_INT 0x0d - -#define WAV_HEADER_SIZE 44 -#define SOUND_FREQUENCY 11025 - -void interrupt (*old_dma_int)(void); -unsigned char end_of_sound = 1; - -int num_of_loaded_sounds = 0; - - int sb_init(void) { return 1; @@ -71,56 +48,18 @@ void play_sound(SOUND* s) unsigned get_file_size(char* filename) { - FILE* p; - unsigned size = 0; - - if ((p = fopen(filename, "r+b")) == NULL) - return NULL; - - while (!feof(p)) { - fgetc(p); - size++; - } - - fclose(p); - return size; + return 0; } int load_sound(SOUND* s, char* filename) { - FILE* p; - unsigned i; - - if ((s->lenght = get_file_size(filename)) == NULL) { - strcpy(missed_file, filename); - return NULL; - } - - //s->lenght-=WAV_HEADER_SIZE; - - if ((s->buffer = (unsigned char*)malloc(s->lenght)) == NULL) - return NULL; - - p = fopen(filename, "r+b"); - - //fseek(p,WAV_HEADER_SIZE,SEEK_SET); - fgets(s->buffer, s->lenght, p); - - fclose(p); - - num_of_loaded_sounds++; return 1; } void unload_sound(SOUND* s) { - free(s->buffer); } void unload_sounds(void) { - int i; - - for (i = 0; i < num_of_loaded_sounds; i++) - unload_sound(&soundtab[i]); } diff --git a/stars.c b/stars.c index 05b86da..34d63e6 100644 --- a/stars.c +++ b/stars.c @@ -30,8 +30,8 @@ void g_inicjuj(void) for (i = 0; i < 50; i++) { g = &gwiazda[i]; - g->x = random(320); - g->y = random(200); + g->x = rand() % 320; + g->y = rand() % 200; if (i < 10) { g->v = 0; @@ -73,7 +73,7 @@ void gwiazdy(void) g->y++; } if (g->y > 199) { - g->x = random(320); + g->x = rand() % 320; g->y = 0; } }