A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Revision:
31:ab24d028ddfd
Parent:
30:ec915d24d3e9
Child:
32:fe6359ef9916
--- a/main.cpp	Sun May 05 18:04:43 2019 +0000
+++ b/main.cpp	Sun May 05 18:37:23 2019 +0000
@@ -25,139 +25,194 @@
 #define MAX_ROOMS_MAP_X 11
 #define MAX_ROOMS_MAP_Y 11
 
+// Variables
+int title_count = 0;
+int cursor_timer = 20;
+int title_option = 0;
+float global_contrast = 0.5;
+
 // Objects
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad gamepad;
 
 // Prototypes
-void render();
+void title_screen();
+void draw_title_screen(N5110 &lcd);
+void title_options_joystick(Gamepad &gamepad);
+
+void title_option_option();
+void title_option_credit();
+void title_option_hi_scores();
+void game_loop();
+void game_over();
+Player *player;
+Room *rooms[MAX_ROOMS_MAP_Y][MAX_ROOMS_MAP_X];
+RoomEngine *room_engine;
 
 // Functions
 int main()
 {
     // Initialize
-    float global_contrast = 0.5;
     lcd.init();
     lcd.setContrast(global_contrast); 
     gamepad.init();
-    Room *rooms[MAX_ROOMS_MAP_Y][MAX_ROOMS_MAP_X];
     
     while(1) { // Gameloop
-        int title_count = 0;
-        int cursor_timer = 20;
-        int title_option = 0;
-        Player demo_player(5, 36);
-        while(1){  // Title Screen Loop
+        
+        player = new Player(5, 36);
+        title_screen();
+        delete player;
+        
+        srand(title_count);
+        player = new Player(39, 27);
+        room_engine = new RoomEngine(global_contrast);
+        
+        game_loop();
+        game_over();
+        delete room_engine;
+        delete player;
+    }
+}
+
+void draw_title_screen(N5110 &lcd)
+{
+    lcd.drawSprite(11, 4, 15, 44, (int *)title_name_0);
+    lcd.drawSpriteTransparent(19, 14, 17, 53, (int *)title_name_1);
+    lcd.drawCircle(79, 7, 10, FILL_BLACK);
+    lcd.drawCircle(81, 5, 8, FILL_WHITE);
+    lcd.drawSprite(56, 6, 11, 5, (int *)star_sprite[abs(((title_count/20) % 7) - 3)]);
+    lcd.drawSprite(12, 34, 8, 8, (int *)button_A_sprite);
+    lcd.drawSprite(22, 37, 3, 2, (int *)arrow_left_sprite);
+    lcd.drawSprite(59, 37, 3, 2, (int *)arrow_right_sprite);
+    lcd.drawSprite(69, 31, 12, 6, (int *)sprite_player[(title_count/40) % 4][(title_count/10) % 4]);
+    lcd.drawSprite(26, 35, 9, 32, (int *)title_options_sprite[title_option]);
+}
+
+void title_options_joystick(Gamepad &gamepad)
+{
+    if ((gamepad.get_direction() == 3) && (cursor_timer > 20)) {    // Detect Joystick going right
+        cursor_timer = 0;
+        if (title_option >= 3) {
             title_option = 0;
-            while(!gamepad.check_event(Gamepad::A_PRESSED)){ 
-                lcd.clear();
-                lcd.drawSprite(11, 4, 15, 44, (int *)title_name_0);
-                lcd.drawSpriteTransparent(19, 14, 17, 53, (int *)title_name_1);
-                lcd.drawCircle(79, 7, 10, FILL_BLACK);
-                lcd.drawCircle(81, 5, 8, FILL_WHITE);
-                lcd.drawSprite(56, 6, 11, 5, (int *)star_sprite[abs(((title_count/20) % 7) - 3)]);
-                lcd.drawSprite(12, 34, 8, 8, (int *)button_A_sprite);
-                lcd.drawSprite(22, 37, 3, 2, (int *)arrow_left_sprite);
-                lcd.drawSprite(59, 37, 3, 2, (int *)arrow_right_sprite);
-                lcd.drawSprite(69, 31, 12, 6, (int *)sprite_player[(title_count/40) % 4][(title_count/10) % 4]);
-                lcd.drawSprite(26, 35, 9, 32, (int *)title_options_sprite[title_option]);
-                lcd.refresh();
-                
-                if ((gamepad.get_direction() == 3) && (cursor_timer > 20)) {    // Detect Joystick going right
-                    cursor_timer = 0;
-                    if (title_option >= 3) {
-                        title_option = 0;
-                    } else {
-                        title_option++;
-                    }
-                } else if ((gamepad.get_direction() == 7) && (cursor_timer > 20)) { // Detect Joystick going left
-                    cursor_timer = 0;
-                    if (title_option <= 0) {
-                        title_option = 3;
-                    } else {
-                        title_option--;
-                    }
-                }
-                
-                cursor_timer++;
-                title_count++;
-                wait_ms(1000/40); // 1s/framerate
+        } else {
+            title_option++;
+        }
+    } else if ((gamepad.get_direction() == 7) && (cursor_timer > 20)) { // Detect Joystick going left
+        cursor_timer = 0;
+        if (title_option <= 0) {
+            title_option = 3;
+        } else {
+            title_option--;
+        }
+    }
+    cursor_timer++;
+}
+
+void title_screen()
+{
+    while(1){  // Title Screen Loop
+        title_option = 0;
+        while(!gamepad.check_event(Gamepad::A_PRESSED)){ 
+            lcd.clear();
+            draw_title_screen(lcd);
+            lcd.refresh();
+            
+            title_options_joystick(gamepad);
+            
+            title_count++;
+            wait_ms(1000/40); // 1s/framerate
+        }
+        while(gamepad.check_event(Gamepad::A_PRESSED)){}
+        if(title_option == 0) {
+            break;
+        } else if (title_option == 1) {
+            while(!gamepad.check_event(Gamepad::A_PRESSED)) {
+                title_option_option();
             }
-            while(gamepad.check_event(Gamepad::A_PRESSED)){}
-            if(title_option == 0) {
-                break;
-            } else if (title_option == 1) {
-                while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-                    global_contrast = gamepad.read_pot();
-                    lcd.setContrast(global_contrast);
-                    lcd.clear();
-                    lcd.printString("Set contrast", 0, 0);
-                    lcd.printString("using the", 0, 1);
-                    lcd.printString("potentiometer", 0, 2);
-                    demo_player.draw(lcd);
-                    lcd.refresh();
-                    demo_player.move(1, 0, (int *)level_map[0][0], (bool *)sprite_transparent_player);
-                    demo_player.buttons(false, true, false, false);
-                    demo_player.update_bullets((int *)level_map[0][0], (bool *)sprite_transparent_player);
-                    if(demo_player.get_pos_x() >= 84){
-                        demo_player.set_position(-6 ,demo_player.get_pos_y());
-                    }
-                    wait_ms(1000/40);
-                }
-                wait(0.05);
-                while(gamepad.check_event(Gamepad::A_PRESSED)) {
-                }
-            } else if (title_option == 2) {
-                lcd.clear();
-                lcd.setContrast(global_contrast);
-                lcd.printString("Made by:", 0, 0);
-                lcd.printString("Steven Mahasin", 0, 1);
-                lcd.printString("201192939", 0, 2);
-                lcd.refresh();
-                wait(0.05);
-                while(!gamepad.check_event(Gamepad::A_PRESSED)) {
-                }
-                wait(0.05);
-                while(gamepad.check_event(Gamepad::A_PRESSED)) {
-                }
-            } else if (title_option == 3) {
-                
+            wait(0.05);
+            while(gamepad.check_event(Gamepad::A_PRESSED)) {
+            }
+        } else if (title_option == 2) {
+            title_option_credit();
+            wait(0.05);
+            while(!gamepad.check_event(Gamepad::A_PRESSED)) {
+            }
+            wait(0.05);
+            while(gamepad.check_event(Gamepad::A_PRESSED)) {
+            }
+        } else if (title_option == 3) {
+            
+        }
+    }
+}
+
+void title_option_option()
+{
+    global_contrast = gamepad.read_pot();
+    lcd.setContrast(global_contrast);
+    lcd.clear();
+    lcd.printString("Set contrast", 0, 0);
+    lcd.printString("using the", 0, 1);
+    lcd.printString("potentiometer", 0, 2);
+    player->draw(lcd);
+    lcd.refresh();
+    player->move(1, 0, (int *)level_map[0][0], (bool *)sprite_transparent_player);
+    player->buttons(false, true, false, false);
+    player->update_bullets((int *)level_map[0][0], (bool *)sprite_transparent_player);
+    wait_ms(1000/40);
+}
+
+void title_option_credit()
+{
+    lcd.clear();
+    lcd.setContrast(global_contrast);
+    lcd.printString("Made by:", 0, 0);
+    lcd.printString("Steven Mahasin", 0, 1);
+    lcd.printString("201192939", 0, 2);
+    lcd.refresh();
+}
+void title_option_hi_scores()
+{
+    
+}
+
+void game_loop()
+{
+    while(1) {  // Floor Loop 
+        for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
+            for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
+                rooms[j][i] = new Room(2);
             }
         }
-        demo_player.~Player();
-        srand(title_count);
-        Player player(39, 27);
-        RoomEngine room_engine(global_contrast);
-        
-        while(1) {  // Floor Loop 
-            for (int i = 0; i < MAX_ROOMS_MAP_X; i++) {
-                for (int j = 0; j < MAX_ROOMS_MAP_Y; j++) {
-                    rooms[j][i] = new Room(2);
+    
+        // Rooms Generation
+        while(1) {  // Room Loop
+            rooms[room_engine->get_room_y()][room_engine->get_room_x()]->load();
+            room_engine->load(player, rooms[room_engine->get_room_y()][room_engine->get_room_x()]);
+            
+            room_engine->entrance_scene(lcd, gamepad);
+            while(room_engine->check_player_room_position() == INSIDE) {  // Room actions
+                room_engine->read_input(gamepad);
+                room_engine->update();
+                room_engine->render(lcd, gamepad);
+                if  (player->get_hp() <= 0) {
+                    goto gameover;
                 }
             }
+            room_engine->exit_scene(lcd, gamepad);
+            rooms[room_engine->get_room_y()][room_engine->get_room_x()]->unload();
+            player->delete_bullets();
+            room_engine->update_current_room();
+        }
+    }
+    gameover : {
+        game_over();
+    }
+}
 
-            // Rooms Generation
-            while(1) {  // Room Loop
-                rooms[room_engine.get_room_y()][room_engine.get_room_x()]->load();
-                room_engine.load(player, rooms[room_engine.get_room_y()][room_engine.get_room_x()]);
-                
-                room_engine.entrance_scene(lcd, gamepad);
-                while(room_engine.check_player_room_position() == INSIDE) {  // Room actions
-                    room_engine.read_input(gamepad);
-                    room_engine.update();
-                    room_engine.render(lcd, gamepad);
-                    if  (player.get_hp() <= 0) {
-                        goto gameover;
-                    }
-                }
-                room_engine.exit_scene(lcd, gamepad);
-                rooms[room_engine.get_room_y()][room_engine.get_room_x()]->unload();
-                player.delete_bullets();
-                room_engine.update_current_room();
-            }
-        }
-        gameover : {
-            while(1){   // Game Over Screen Loop
+void game_over()
+{
+    while(1){   // Game Over Screen Loop
                 lcd.clear();
                 lcd.setContrast(global_contrast);
                 lcd.printString("Game Over", 0, 0);
@@ -171,8 +226,4 @@
                 }
                 break;
             }
-            room_engine.~RoomEngine();
-            player.~Player();
-        }
-    }
 }
\ No newline at end of file