Skip to content

Commit

Permalink
finished music! Now ready for release :D
Browse files Browse the repository at this point in the history
  • Loading branch information
pepsipu committed Jan 12, 2020
1 parent fa09237 commit 66eba0c
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 31 deletions.
Binary file modified build/kernel/kernel.bin
Binary file not shown.
Binary file modified build/kernel/kernel.elf
Binary file not shown.
Binary file modified build/kernel/kernel.o
Binary file not shown.
Binary file modified build/xinos.img
Binary file not shown.
32 changes: 32 additions & 0 deletions src/games/XinMusic/xm_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <kernel/vesa/graphics.c>
#include <music/music_list.h>
#include <kernel/utils/misc.c>
#include <kernel/utils/rand.c>

#define XM_BIG_MAIN_COLOR 0x363333 // 0x2f3032
#define XM_BIG_SECOND_COLOR 0x272121 // 0x383a56
#define XM_BIG_HIGHLIGHT_2 0xe16428 // 0xb0a565
#define XM_BIG_HIGHLIGHT_1 0xf6e9e9 // 0xede68a

// shift the RBG888 colors into RBG565, with a small loss of detail
#define XM_MAIN_COLOR (((XM_BIG_MAIN_COLOR&0xf80000)>>8) + ((XM_BIG_MAIN_COLOR&0xfc00)>>5) + ((XM_BIG_MAIN_COLOR&0xf8)>>3))
#define XM_SECOND_COLOR (((XM_BIG_SECOND_COLOR&0xf80000)>>8) + ((XM_BIG_SECOND_COLOR&0xfc00)>>5) + ((XM_BIG_SECOND_COLOR&0xf8)>>3))
#define XM_HIGHLIGHT_2 (((XM_BIG_HIGHLIGHT_2&0xf80000)>>8) + ((XM_BIG_HIGHLIGHT_2&0xfc00)>>5) + ((XM_BIG_HIGHLIGHT_2&0xf8)>>3))
#define XM_HIGHLIGHT_1 (((XM_BIG_HIGHLIGHT_1&0xf80000)>>8) + ((XM_BIG_HIGHLIGHT_1&0xfc00)>>5) + ((XM_BIG_HIGHLIGHT_1&0xf8)>>3))

#define RAND "Now playing a random song."
#define XM_TITLE "XinMusic"

int xm_main() {
uint32_t song_index = max_rand(sizeof(songs) / sizeof(struct song_entry) - 1);
draw_background(XM_SECOND_COLOR);
draw_square_size(center_x(160, vbe_info->width), 20, 40, 160, XM_MAIN_COLOR);
draw_string(XM_TITLE, center_x(string_len(XM_TITLE) * 8, vbe_info->width), 32, XM_HIGHLIGHT_1);
draw_square_size(center_x(190, vbe_info->width), 80, 80, 190, XM_MAIN_COLOR);
draw_string(songs[song_index].song_name, center_x(string_len(songs[song_index].song_name) * 8, vbe_info->width), 80 + 16, XM_HIGHLIGHT_2);
draw_string(songs[song_index].porter, center_x(string_len(songs[song_index].porter) * 8, vbe_info->width), 80 + 40, XM_HIGHLIGHT_1);
draw_string(RAND, center_x(string_len(RAND) * 8, vbe_info->width), vbe_info->height / 2, XM_HIGHLIGHT_1);
play_song_by_index(song_index);
exit_to_main();
return 0;
}
45 changes: 20 additions & 25 deletions src/games/XinSnake/xa_main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <kernel/vesa/graphics.c>
#include <music/music_list.h>
#include <kernel/utils/misc.c>
#include <kernel/utils/bmp_loader.c>
#include <kernel/utils/rand.c>

#define XA_BIG_MAIN_COLOR 0x363333 // 0x2f3032
#define XA_BIG_SECOND_COLOR 0x272121 // 0x383a56
#define XA_BIG_HIGHLIGHT_2 0xe16428 // 0xb0a565
#define XA_BIG_HIGHLIGHT_1 0xf6e9e9 // 0xede68a
#define XS_BIG_MAIN_COLOR 0x363333 // 0x2f3032
#define XS_BIG_SECOND_COLOR 0x272121 // 0x383a56
#define XS_BIG_HIGHLIGHT_2 0xe16428 // 0xb0a565
#define XS_BIG_HIGHLIGHT_1 0xf6e9e9 // 0xede68a

// shift the RBG888 colors into RBG565, with a small loss of detail
#define XA_MAIN_COLOR (((XA_BIG_MAIN_COLOR&0xf80000)>>8) + ((XA_BIG_MAIN_COLOR&0xfc00)>>5) + ((XA_BIG_MAIN_COLOR&0xf8)>>3))
#define XA_SECOND_COLOR (((XA_BIG_SECOND_COLOR&0xf80000)>>8) + ((XA_BIG_SECOND_COLOR&0xfc00)>>5) + ((XA_BIG_SECOND_COLOR&0xf8)>>3))
#define XA_HIGHLIGHT_2 (((XA_BIG_HIGHLIGHT_2&0xf80000)>>8) + ((XA_BIG_HIGHLIGHT_2&0xfc00)>>5) + ((XA_BIG_HIGHLIGHT_2&0xf8)>>3))
#define XA_HIGHLIGHT_1 (((XA_BIG_HIGHLIGHT_1&0xf80000)>>8) + ((XA_BIG_HIGHLIGHT_1&0xfc00)>>5) + ((XA_BIG_HIGHLIGHT_1&0xf8)>>3))
#define XS_MAIN_COLOR (((XS_BIG_MAIN_COLOR&0xf80000)>>8) + ((XS_BIG_MAIN_COLOR&0xfc00)>>5) + ((XS_BIG_MAIN_COLOR&0xf8)>>3))
#define XS_SECOND_COLOR (((XS_BIG_SECOND_COLOR&0xf80000)>>8) + ((XS_BIG_SECOND_COLOR&0xfc00)>>5) + ((XS_BIG_SECOND_COLOR&0xf8)>>3))
#define XS_HIGHLIGHT_2 (((XS_BIG_HIGHLIGHT_2&0xf80000)>>8) + ((XS_BIG_HIGHLIGHT_2&0xfc00)>>5) + ((XS_BIG_HIGHLIGHT_2&0xf8)>>3))
#define XS_HIGHLIGHT_1 (((XS_BIG_HIGHLIGHT_1&0xf80000)>>8) + ((XS_BIG_HIGHLIGHT_1&0xfc00)>>5) + ((XS_BIG_HIGHLIGHT_1&0xf8)>>3))

#define TITLE "XinSnake"
#define SUBTITLE "A game to demonstrate the abilities of xinOS"
Expand Down Expand Up @@ -45,7 +44,7 @@ struct {

uint8_t volatile exit_flag = 0;

void xa_key_handler(char key) {
void xs_key_handler(char key) {
switch (key) {
// w, d, s, a for setting snake position
// if statements prevent the snake from flipping directions
Expand Down Expand Up @@ -76,11 +75,6 @@ void xa_key_handler(char key) {
}
}

void game_over() {
draw_string(GAME_OVER, center_x(string_len(GAME_OVER) * 8, vbe_info->width), 200, 0xb000);
wait(1000);
}

// see if snake hit itself
int check_overlap() {
struct snake_node *ptr = snake.next;
Expand All @@ -93,14 +87,14 @@ int check_overlap() {
return 0;
}

int xa_main() {
draw_background(XA_MAIN_COLOR);
draw_string(TITLE, center_x(string_len(TITLE) * 8, vbe_info->width), 50, XA_HIGHLIGHT_2);
draw_string(SUBTITLE, center_x(string_len(SUBTITLE) * 8, vbe_info->width), 70, XA_HIGHLIGHT_1);
draw_string(LOADING, center_x(string_len(LOADING) * 8, vbe_info->width), 200, XA_HIGHLIGHT_1);
draw_line(70, 0, 70, vbe_info->height, XA_SECOND_COLOR, 20);
draw_line(vbe_info->width - 70 - 20, 0, vbe_info->width - 70 - 20, vbe_info->height, XA_SECOND_COLOR, 20);
key_down_handler = xa_key_handler;
int xs_main() {
draw_background(XS_MAIN_COLOR);
draw_string(TITLE, center_x(string_len(TITLE) * 8, vbe_info->width), 50, XS_HIGHLIGHT_2);
draw_string(SUBTITLE, center_x(string_len(SUBTITLE) * 8, vbe_info->width), 70, XS_HIGHLIGHT_1);
draw_string(LOADING, center_x(string_len(LOADING) * 8, vbe_info->width), 200, XS_HIGHLIGHT_1);
draw_line(70, 0, 70, vbe_info->height, XS_SECOND_COLOR, 20);
draw_line(vbe_info->width - 70 - 20, 0, vbe_info->width - 70 - 20, vbe_info->height, XS_SECOND_COLOR, 20);
key_down_handler = xs_key_handler;
snake.x = 0;
snake.y = 0;
direction = 1;
Expand Down Expand Up @@ -139,7 +133,8 @@ int xa_main() {
break;
}
if (vbe_info->width < snake.x + MOVE_AMOUNT || snake.x < 0 || vbe_info->height < snake.y + MOVE_AMOUNT || snake.y < 0 || exit_flag || check_overlap()) {
game_over();
draw_string(GAME_OVER, center_x(string_len(GAME_OVER) * 8, vbe_info->width), 200, 0xb000);
play_song("snake_lose");
ptr = snake.next; // free all segments from tail to head
while (ptr != &snake) {
free(ptr);
Expand All @@ -151,7 +146,7 @@ int xa_main() {
return 1;
}
if (snake.x == fruit.x * MOVE_AMOUNT && snake.y == fruit.y * MOVE_AMOUNT) {
beep(200, 200, 0);
beep(200, 100, 0);
fruit.x = max_rand(vbe_info->width / MOVE_AMOUNT - 1); // will multiply by 32 later, but this is needed to ensure the fruit is aligned
fruit.y = max_rand(vbe_info->height / MOVE_AMOUNT - 1); // will multiply by 32 later, but this is needed to ensure the fruit is aligned
// allocate new tail
Expand Down
5 changes: 3 additions & 2 deletions src/games/game_list.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <games/XinSnake/xa_main.c>
#include <games/XinMusic/xm_main.c>

struct game {
char *name;
Expand All @@ -7,6 +8,6 @@ struct game {
};

struct game games[] = {
{.name = "xinSnake", .author = "pepsipu", .init = xa_main},
{.name = "xinMusic", .author = "pepsipu", .init = 0}
{.name = "xinSnake", .author = "pepsipu", .init = xs_main},
{.name = "xinMusic", .author = "pepsipu", .init = xm_main}
};
7 changes: 6 additions & 1 deletion src/kernel/utils/rand.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef RANDOM
#define RANDOM

uint32_t rdtsc(){
uint64_t ret;
asm volatile ("rdtsc":"=A"(ret));
Expand All @@ -8,4 +11,6 @@ uint16_t max_rand(int max)
{
uint64_t random_seed = rdtsc() * 9679 + 21269;
return (uint16_t) (random_seed / 65536) % (max+1);
}
}

#endif
12 changes: 11 additions & 1 deletion src/kernel/utils/strings.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#ifndef STRINGS
#define STRINGS

#include <kernel/allocator.c>


int string_len(char* s) {
int i = 0;
while (s[i] != '\0') i++;
return i;
}

int string_cmp(char *s1, char *s2) {
int len = string_len(s1);
for (int i = 0; i < len; i++) {
if (s1[i] != s2[i] || s2[i] == '\0') {
return 0;
}
}
return 1;
}

void reverse(char *s) {
int start = 0;
int end = string_len(s) - 1;
Expand Down
25 changes: 23 additions & 2 deletions src/music/music_list.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#ifndef MUSIC
#define MUSIC

#include "raw_frequencies/mario.c"
#include "raw_frequencies/axelf.c"
#include "raw_frequencies/tetris.c"
#include "raw_frequencies/russian_anthem.c"
#include "raw_frequencies/merry_christmas.c"
#include "raw_frequencies/lose.c"
#include "beep_structure.c"
#include <kernel/sound/pc_speaker.c>
#include <kernel/utils/strings.c>

struct {
struct song_entry {
char *song_name;
char *porter;
uint32_t song_length;
Expand Down Expand Up @@ -41,11 +46,27 @@ struct {
.song_name = "christmas",
.porter = "ararouge",
.song_length = sizeof(merry_christmas) / sizeof(struct beep)
},
{
.frequencies = lose,
.song_name = "snake_lose",
.porter = "pepsipu",
.song_length = sizeof(lose) / sizeof(struct beep)
}
};
void play_song(int index) {
void play_song_by_index(int index) {
for (uint32_t i = 0; i < songs[index].song_length; i++) {
struct beep current_beep = songs[index].frequencies[i];
beep(current_beep.freq, current_beep.len, current_beep.delay);
}
}
void play_song(char *name) {
for (uint32_t i = 0; i < sizeof(songs) / sizeof(struct song_entry); i++) {
if (string_cmp(name, songs[i].song_name)) {
play_song_by_index(i);
return;
}
}
}

#endif
31 changes: 31 additions & 0 deletions src/music/raw_frequencies/lose.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../beep_structure.c"

struct beep lose[] = {
{.freq = 130, .len = 100, .delay = 0},
{.freq = 262, .len = 100, .delay = 0},
{.freq = 330, .len = 100, .delay = 0},
{.freq = 392, .len = 100, .delay = 0},
{.freq = 523, .len = 100, .delay = 0},
{.freq = 660, .len = 100, .delay = 0},
{.freq = 784, .len = 300, .delay = 0},
{.freq = 660, .len = 300, .delay = 0},
{.freq = 146, .len = 100, .delay = 0},
{.freq = 262, .len = 100, .delay = 0},
{.freq = 311, .len = 100, .delay = 0},
{.freq = 415, .len = 100, .delay = 0},
{.freq = 523, .len = 100, .delay = 0},
{.freq = 622, .len = 100, .delay = 0},
{.freq = 831, .len = 300, .delay = 0},
{.freq = 622, .len = 300, .delay = 0},
{.freq = 155, .len = 100, .delay = 0},
{.freq = 294, .len = 100, .delay = 0},
{.freq = 349, .len = 100, .delay = 0},
{.freq = 466, .len = 100, .delay = 0},
{.freq = 588, .len = 100, .delay = 0},
{.freq = 699, .len = 100, .delay = 0},
{.freq = 933, .len = 300, .delay = 0},
{.freq = 933, .len = 100, .delay = 0},
{.freq = 933, .len = 100, .delay = 0},
{.freq = 933, .len = 100, .delay = 0},
{.freq = 1047, .len = 400, .delay = 0},
};

0 comments on commit 66eba0c

Please sign in to comment.