News:

Welcome to RetroCoders Community

Main Menu

source code in C for my demo "white night"

Started by ron77, Jun 07, 2023, 08:14 PM

Previous topic - Next topic

ron77

okay, I made a small demo program called "white night" It's basically a story with sound. The text is in text files, and the sound is copyrighted music, so I guess I cannot attach the zip folder to the post, so I just bring the code in C (the program uses Bass Lib for sound)

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <math.h>
#include <windows.h>
#include "bass.h"

#define evening "start.m4a"

#define midnight "mid.mp3"

#define dawn "dawn.mp3"

const char* f0 = "opening.txt";
const char* f1 = "6PM.txt";
const char* f2 = "7PM.txt";
const char* f3 = "8PM.txt";
const char* f4 = "9PM.txt";
const char* f5 = "10PM.txt";
const char* f6 = "11PM.txt";
const char* f7 = "12PM.txt";
const char* f8 = "1AM.txt";
const char* f9 = "2AM.txt";
const char* f10 = "3AM.txt";
const char* f11 = "4AM.txt";
const char* f12 = "5AM.txt";

HSTREAM stream = 0;

void playMusic(const char* path) {
	 // BASS_SetVolume(1);
    stream=BASS_StreamCreateFile(false,path , 0, 0, BASS_SAMPLE_LOOP);
    BASS_ChannelPlay(stream, false);
	// system("pause");
}

void stopMusic() {
	BASS_ChannelFree(stream);
}

void freeMusic() {
	BASS_StreamFree(stream);
}

void bassFree() {
	BASS_Free();
}

char* read(const char* filename){
    FILE* f = fopen(filename, "rb");
    if (f == NULL){
        printf("file %s is not found or missing!", filename);
		exit(1);
    }
    fseek(f, 0L, SEEK_END);
    long size = ftell(f)+1;
    fclose(f);
    f = fopen(filename, "r");
    void* content = memset(malloc(size), '\0', size);
    fread(content, 1, size-1, f);
    fclose(f);
    return (char*) content;
}

void theme(const char* file) {
	system("cls");
	printf("%s", read(file));
	system("pause");
}

void changeMusic(const char* file) {
	system("cls");
	stopMusic();
	freeMusic();
	playMusic(file);
}

int main() {
	
	BASS_Init(-1, 44100, 0, 0, NULL);
	
	theme(f0);
	playMusic(evening);
	theme(f1);
	theme(f2);
	theme(f3);
	theme(f4);
	
	changeMusic(midnight);
	theme(f5);
	theme(f6);
	theme(f7);
	theme(f8);
		
	changeMusic(dawn);
	theme(f9);
	theme(f10);
	theme(f11);
	theme(f12);
	
	system("cls");
	stopMusic();
	freeMusic();
	
	printf("\n\nTHE END.");
	bassFree();
	
	
	
	return 0;
}