our_dick/src/main.cpp

164 lines
3.7 KiB
C++

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <string>
#include <thread>
#include <chrono>
#include <atomic>
#include "render.hpp"
#include "game_state.hpp"
#include "math/math.hpp"
#include "config.hpp"
#include "audio/sndrd.hpp"
#include "audio/mixdata.hpp"
#include "audio/mixer.hpp"
// 0 | 1 | 2
// ---------
// 3 | 4 | 5
// ---------
// 6 | 7 | 8
#define TILE_COUNT 9
namespace{
bool running = true;
render_manager manager;
}
int get_player_input(){
//TODO get player input
return rand() % 9;
}
int check_win_condition(const game_state& gs, char player){
int i;
for(i = 0 ; i < 3; i++){
//rows
if(gs.board[(i*3)] == player && gs.board[(i*3)+1] == player && gs.board[(i*3)+2] == player)
return 1;
//column
if(gs.board[i] == player && gs.board[i+3] == player && gs.board[i+6] == player)
return 1;
}
if(gs.board[0] == player && gs.board[4] == player && gs.board[8] == player)
return 1;
if(gs.board[2] == player && gs.board[4] == player && gs.board[6] == player)
return 1;
return 0;
}
void display_game_state(const game_state& gs){
printf("turn %i:\n", gs.turn);
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(gs.board[(i*3)+j])
printf("%c", gs.board[(i*3)+j]);
else
printf("_");
}
printf("\n");
}
}
void player_turn(game_state& gs, char player, int input){
if(input > TILE_COUNT && input < 0)
fprintf(stderr,"ERR: player input not in range.");
if(!gs.board[input])
gs.board[input] = player;
if(check_win_condition(gs, player)){
printf("player %c wins!\n", player);
gs.turn = -1;
}
}
void game_turn(game_state& gs,int player_input){
gs.turn++;
player_turn(gs, 'O', player_input);
display_game_state(gs);
if(gs.turn == -1)
return;
player_turn(gs, 'X', rand() % TILE_COUNT);
display_game_state(gs);
}
int exists_empty_tile(const game_state& gs){
int i;
for(i = 0; i < TILE_COUNT; i++)
if(!gs.board[i])
return 1;
return 0;
}
void handle_window_close(){
debug_print("[II] Window close event triggered\n");
running = false;
}
void handle_input_events(GLFWwindow*, int key, int, int, int){
debug_print("[II] Recieved keycode %d\n", key);
if(key == 256)
manager.request_exit();
}
audio::mixchunk read_audio_file(const char* filename){
debug_print("!!!!filename!!!! %s\n", filename);
audio::sndrd f(filename, audio::sndrd::mode::r);
if(!f.valid()){
return {};
}
return f.read_all();
}
std::string select_audio_file(){
size_t filecnt = 0;
for(std::filesystem::directory_iterator i("assets/moans");i != std::filesystem::directory_iterator();++i){
std::filesystem::directory_entry e = *i;
if(!e.is_directory())
++filecnt;
}
size_t selection = rand() % filecnt;
for(std::filesystem::directory_iterator i("assets/moans");i != std::filesystem::directory_iterator();++i){
std::filesystem::directory_entry e = *i;
if(selection == 0){
return e.path().native();
}
if(!e.is_directory())
--selection;
}
return {};
}
void gay_thread(audio::mixer& m, std::atomic_bool& should_gay_thread_stop){
while(!should_gay_thread_stop){
audio::mixchunk ch = read_audio_file(select_audio_file().c_str());
m.get_channel(0).play(ch);
std::this_thread::sleep_for(std::chrono::seconds(3));
}
}
int main(){
srand(time(NULL));
audio::mixer mix(audio::mixer::mode::STEREO, 1);
game_state gs = {};
manager.init(640, 480, "Tic-Tac-Gugh");
manager.handle_window_close_event(handle_window_close);
manager.handle_keypress_event(handle_input_events);
std::atomic_bool should_gay_thread_stop = false;
std::thread t(gay_thread, std::ref(mix), std::ref(should_gay_thread_stop));
while(running)
manager.update();
should_gay_thread_stop = true;
while(exists_empty_tile(gs) && gs.turn != -1){
game_turn(gs, get_player_input());
}
t.join();
}