Delivered-To: mlmmj-tyndur-devel
Received: from sv1.famkaufmann.info [176.9.73.67]
	by a9b8c5461646 with IMAP (fetchmail-6.3.26)
	for <mlmmj@localhost> (single-drop); Fri, 05 Mar 2021 09:36:30 +0000 (GMT)
Received: from localhost ([172.29.0.7])
	by sv1.famkaufmann.info with LMTP
	id gOByJ577QWDRYQAALHnFKQ
	(envelope-from <mayernico256@gmail.com>)
	for <mlmmj-tyndur-devel>; Fri, 05 Mar 2021 09:36:30 +0000
Delivered-To: tyndur-devel@tyndur.org
Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [2a00:1450:4864:20::630])
	by sv1.famkaufmann.info (OpenSMTPD) with ESMTPS id a385de34 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO)
	for <tyndur-devel@tyndur.org>;
	Fri, 5 Mar 2021 09:36:29 +0000 (UTC)
Received: by mail-ej1-x630.google.com with SMTP id hs11so2183485ejc.1
        for <tyndur-devel@tyndur.org>; Fri, 05 Mar 2021 01:36:29 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=from:to:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=UBecwfJRJfIhFzjQaYVc+ohAcbuKClcQklFJthv7+QM=;
        b=F2H7hI8Pa7rXB5+NOpc5XTRGQS3Cm4ce1tOasVy80VJsOt2F8T/aPx/bOBKV2Gq9JH
         ZrcjprQVQ0a9Kwknk8f984vim9GgaiqVqNK2ZMizHDwP+K8G7RJr4iZsfLmkNnaRajwk
         EIe1XKi3X4zJd0tj/PfejzCAXQ+5gFSPjypxJK1DhwmKgc932MNexszNFT+yyKpPfZPS
         ttS4/1GbjopmYL3ESLl2nulF3C/0040CvPsyIekvEQKuFNBkdMHEINGi1GedeVsbf6eZ
         y12XAe5veNll07iNCSL5q9Qv7cm2Jl92bKHYfWtybOqwyw7Q1nEd+lggZRFVVVbxTDek
         tUrg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:from:to:subject:date:message-id:mime-version
         :content-transfer-encoding;
        bh=UBecwfJRJfIhFzjQaYVc+ohAcbuKClcQklFJthv7+QM=;
        b=p3NThUdZprGurR/UeB0ikF8RrQdBdmj1APkM89QWF44NBk3m+FZ0U9+kzIgDVXoDeC
         9trHhFnYzfVDk04gVpJL2bAq4eu/F77Azn07U4c9zKYlBchBgAg57D+vwEvtouo3RRZ7
         w/Zl5U7dUweB3SWRVu0cDu6Yt3OISkNA2/YRkZ5cwz98myS4wX+EjbcbzfmBiOa3E8Ge
         u+TGRep959Yinw9KCfMrVi5Z9O15WHlm39RPq4SVr5UvnyHRXIAp5oQ+qxPxgfxwLw3o
         ug1hKP1EG4xMX9yP77ku4TPINpcAU6mc6Ll8RVEKS47TTi9q5hsejDgb7CO8Sq/lbrAd
         +ahA==
X-Gm-Message-State: AOAM531PKHv1MUPhrkY/xAX8TGOrO8Yiy7DdsnDU59bzDdJz9dB6Ka/3
	AFAls9nCEm1CLJ/ZvdxYqa440qXj0z8=
X-Google-Smtp-Source: ABdhPJxY9+H8Q7ZK8R6wIRP90WtfbylcW7T9b1WTSpktdO6FtrvJfKoPNStVWnoHJmVxHEnRLmRFgw==
X-Received: by 2002:a17:906:644f:: with SMTP id l15mr1546386ejn.229.1614936989479;
        Fri, 05 Mar 2021 01:36:29 -0800 (PST)
Received: from localhost.localdomain (p5b0b7cb5.dip0.t-ipconnect.de. [91.11.124.181])
        by smtp.gmail.com with ESMTPSA id mc2sm1115414ejb.115.2021.03.05.01.36.27
        for <tyndur-devel@tyndur.org>
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 05 Mar 2021 01:36:28 -0800 (PST)
From: Nico Mayer <mayernico256@gmail.com>
X-Google-Original-From: Nico Mayer <mayerNico256@gmail.com>
To: tyndur-devel@tyndur.org
Subject: [PATCH v4] modules: reparieren von Pong
Date: Fri,  5 Mar 2021 10:36:16 +0100
Message-Id: <20210305093616.77869-1-mayerNico256@gmail.com>
X-Mailer: git-send-email 2.29.2
Errors-To: toni@tyndur.org
Precedence: list
Reply-To: tyndur-Patches <tyndur-devel@tyndur.org>
List-Id: tyndur-Patches <tyndur-devel.tyndur.org>
List-Unsubscribe: <mailto:tyndur-devel+unsubscribe@tyndur.org>
List-Post: <mailto:tyndur-devel@tyndur.org>
List-Help: <mailto:tyndur-devel+help@tyndur.org>
List-Subscribe: <mailto:tyndur-devel+subscribe@tyndur.org>
List-Owner: <mailto:toni@tyndur.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

From: Nico Mayer <mayerNico@outlook.de>

+ Mit dem Programm Pong kann man jetzt wirklich Pong spielen!

Signed-off-by: Nico Mayer <mayerNico@outlook.de>
---
 src/modules/c/pong/main.c | 342 ++++++++++++++++++++++++++++++++------
 src/modules/include/kbd.h |   2 +
 2 files changed, 289 insertions(+), 55 deletions(-)

diff --git a/src/modules/c/pong/main.c b/src/modules/c/pong/main.c
index 0773dc21..7ea63ef1 100644
--- a/src/modules/c/pong/main.c
+++ b/src/modules/c/pong/main.c
@@ -33,15 +33,264 @@
 #include <syscall.h>
 #include <types.h>
 #include <unistd.h>
+#include <init.h>
+#include <kbd.h>
+#include <lostio.h>
 
-int main(int argc, char** argv)
+//------------------------------------------------------------------------------
+// Types und Structs
+//------------------------------------------------------------------------------
+
+typedef struct Vector2 {
+    int x;
+    int y;
+} Vector2;
+
+typedef struct Player {
+    Vector2 position;
+    Vector2 size;
+    int lives;
+} Player;
+
+typedef struct Ball {
+    Vector2 position;
+    Vector2 speed;
+    Vector2 size;
+    bool active;
+} Ball;
+
+typedef struct Key {
+    bool down;
+    bool pressed;
+} Key;
+
+typedef struct Keyboard {
+    pid_t kbc_pid;
+    Key arrow_up;
+    Key arrow_down;
+    Key enter;
+    Key escape;
+} Keyboard;
+
+//------------------------------------------------------------------------------
+// Gloable Variablen
+//------------------------------------------------------------------------------
+
+static const int screen_width = 640;
+static const int screen_height = 480;
+static const int time_per_frame = 20 * 1000; // 20ms
+static const int player_max_live = 5;
+
+static bool game_over = false;
+
+static Player player = {0};
+static Ball ball = {0};
+static Keyboard keyboard = {0};
+
+static video_bitmap_t* front;
+
+//------------------------------------------------------------------------------
+// Funktions Definitionen
+//------------------------------------------------------------------------------
+
+static void update_key(Key* key, bool down)
+{
+    if (!down && key->down) {
+        key->pressed = true;
+    }
+
+    key->down = down;
+}
+
+static bool is_key_pressed(Key* key)
+{
+    if (key->pressed) {
+        key->pressed = false;
+        return true;
+    }
+
+    return false;
+}
+
+static void update_player(void)
+{
+    if (is_key_pressed(&keyboard.escape)) {
+        game_over = true;
+    }
+
+    if (!game_over) {
+        if (player.lives <= 0) {
+            game_over = true;
+        }
+        if (keyboard.arrow_up.down) {
+            player.position.y -= 5;
+        }
+        if (keyboard.arrow_down.down) {
+            player.position.y += 5;
+        }
+        if ((player.position.y + player.size.y) >= screen_height) {
+            player.position.y = screen_height - player.size.y;
+        }
+        if (player.position.y <= 0) {
+            player.position.y = 0;
+        }
+    }
+}
+
+static void update_ball(void)
+{
+    if (!game_over)
+    {
+        if (is_key_pressed(&keyboard.enter) && !ball.active) {
+            ball.speed = (Vector2){5,0};
+            ball.active = true;
+        }
+        if (ball.active) {
+            ball.position.x += ball.speed.x;
+            ball.position.y += ball.speed.y;
+        }
+        if ((ball.position.y + ball.size.y) >= screen_height ||
+            ball.position.y <= 0)
+        {
+            ball.speed.y = -ball.speed.y;
+        }
+
+        if ((ball.position.x + ball.size.x) >= screen_width) {
+            ball.speed.x = -ball.speed.x;
+        }
+
+        // Tooooor!
+        if (ball.position.x <= 0) {
+            ball.position = (Vector2){screen_width/2, screen_height/2};
+            ball.speed = (Vector2){0, 0};
+            ball.active = false;
+            player.lives--;
+        }
+
+        // kollision mit Spieler
+        // Der Ball kann auch von schräg oben bzw. schräg unten kommen.
+        // Daher wird der gesamte Kollisonsbereich überprüft.
+        if (player.position.x - ball.size.x <= ball.position.x &&
+            player.position.x + player.size.x >= ball.position.x &&
+            player.position.y - ball.size.y <= ball.position.y &&
+            player.position.y + player.size.y >= ball.position.y)
+        {
+            if (ball.speed.x < 0) {
+                ball.speed.x = -ball.speed.x;
+            }
+
+            // Erhöhen der Geschwindigkeit
+            // und damit auch der Schwierigkeitsgrad
+            if (ball.speed.y > 0) {
+                ball.speed.y += 1;
+            }
+            else {
+              ball.speed.y -= 1;
+            }
+
+            ball.speed.x += 1;
+        }
+    }
+}
+
+static void render(void)
+{
+    // Male Hintergrund
+    libvideo_change_color(0, 0, 0, 0);
+    libvideo_draw_rectangle(0, 0, screen_width, screen_height);
+
+    // Male Spieler
+    libvideo_change_color(0, 255, 0, 0);
+    libvideo_draw_rectangle(player.position.x, player.position.y,
+                            player.size.x, player.size.y);
+
+    // Male Ball
+    libvideo_change_color(0, 0xab, 0xcd, 0xef);
+    libvideo_draw_rectangle(ball.position.x, ball.position.y,
+                            ball.size.x, ball.size.y);
+
+    // Male Leben
+    for (int i = 1; i < player.lives; i++) {
+        libvideo_draw_rectangle(screen_width - 40 * i, 16, 20, 20);
+    }
+
+    libvideo_do_command_buffer();
+    libvideo_flip_buffers(front);
+}
+
+static bool init_game(void)
+{
+
+    /* Initialize player */
+    player.position = (Vector2){screen_width*1/10, screen_height/2};
+    player.size = (Vector2){20, screen_height/5};
+    player.lives = player_max_live;
+
+    /* Initialize ball */
+    ball.position = (Vector2){screen_width/2, screen_height/2};
+    ball.speed = (Vector2){0, 0};
+    ball.size = (Vector2){20, 20};
+    ball.active = false;
+
+    return true;
+}
+
+static void rpc_kbd_callback(pid_t pid, uint32_t cid, size_t data_size,
+    void* data)
+{
+    uint8_t* kbd_data = data;
+    char buf[16];
+
+    if ((pid != keyboard.kbc_pid) || (data_size != 2)) {
+        return;
+    }
+
+    p();
+
+    if (kbd_data[0] == KEYCODE_ARROW_UP) {
+        update_key(&keyboard.arrow_up, !kbd_data[1]);
+    } else if (kbd_data[0] == KEYCODE_ARROW_DOWN) {
+        update_key(&keyboard.arrow_down, !kbd_data[1]);
+    } else if (kbd_data[0] == KEYCODE_ENTER) {
+        update_key(&keyboard.enter, !kbd_data[1]);
+    } else if (kbd_data[0] == KEYCODE_ESC) {
+        update_key(&keyboard.escape, !kbd_data[1]);
+    }
+
+    v();
+
+    /*
+     *  Und stdin auslesen, sonst laden alle Tastendrücke nach dem Beenden in
+     *  der Shell. Benutzt werden sie nicht.
+     */
+    if (stdin->res) {
+        lio_readf(stdin->res->lio2_stream, 0, sizeof(buf), buf, LIO_REQ_FILEPOS);
+    }
+
+}
+
+static bool init_keyboard(void)
+{
+    // RPC-Handler fuer die Callbacks von KBD einrichten
+    register_message_handler(KBD_RPC_CALLBACK, rpc_kbd_callback);
+
+    keyboard.kbc_pid = init_service_get("kbc");
+    if (!keyboard.kbc_pid) {
+        return false;
+    }
+
+    // Callback registrtrieren bei KBC
+    return rpc_get_dword(keyboard.kbc_pid, KBD_RPC_REGISTER_CALLBACK, 0, NULL);
+}
+
+static bool init_screen(void)
 {
     stdout = 0;
     driver_context_t* main_context;
     main_context = libvideo_create_driver_context("vesa");
     if (!main_context) {
         fprintf(stderr, "Konnte Grafiktreiberkontext nicht erstellen\n");
-        return EXIT_FAILURE;
+        return false;
     }
     libvideo_use_driver_context(main_context);
     libvideo_get_command_buffer(1024);
@@ -85,75 +334,58 @@ int main(int argc, char** argv)
 
     printf("Farbe gesetzt.\n");
 
-    if (libvideo_change_display_resolution(0, 640, 480, 24) != 0) {
+    if (libvideo_change_display_resolution(0, screen_width,
+                                           screen_height, 24) != 0)
+    {
         printf("Modus setzen fehlgeschlagen\n");
-        return -1;
+        return false;
     }
 
     printf("Modus gesetzt.\n");
 
-    video_bitmap_t *front;
     front = libvideo_get_frontbuffer_bitmap(0);
     int buffers = libvideo_request_backbuffers(front, 2);
 
     printf("Got frontbuffer. %d Backbuffer.\n", buffers);
 
-    int angle = 0;
-    int speed = 100;
+    libvideo_change_target(front);
+    return true;
+}
 
-    int x = 1024/2;
-    int y = 768/2;
+static void close_screen(void)
+{
+    libvideo_close_upload_buffer(front);
+    libvideo_destroy_bitmap(front);
+    libvideo_restore_text_mode();
+}
+static void run(void)
+{
+    uint64_t time_last_update = get_tick_count();
+    uint64_t delta_time = 0;
 
-    int dx, dy;
+    while (!game_over) {
 
-    int frames = 0;
+        uint64_t current_time = get_tick_count();
+        delta_time += current_time - time_last_update;
+        time_last_update = current_time;
 
-    uint64_t start = get_tick_count();
+        while (delta_time >= time_per_frame) {
+            delta_time -= time_per_frame;
+            update_player();
+            update_ball();
+        }
+        render();
+    }
+    close_screen();
+}
 
-    int oldsec = 0;
+int main(int argc, char** argv)
+{
+    init_screen();
+    init_keyboard();
+    init_game();
 
-    libvideo_change_target(front);
+    run();
 
-    while (1) {
-//        libvideo_change_target(back);
-        libvideo_change_color(0,0,0,0);
-        libvideo_draw_rectangle(0,0,1024,768);
-
-        libvideo_change_color(0, 255, 0, 0);
-        libvideo_draw_rectangle(0,0,1024,20);
-        libvideo_draw_rectangle(0, 748, 1024, 20);
-        libvideo_draw_rectangle(1004, 0, 20, 768);
-
-        dx = sin(angle * M_PI / 180) * speed;
-        dy = cos(angle * M_PI / 180) * speed;
-
-        angle += 1;
-        if (angle >= 360) angle=0;
-
-        libvideo_change_color(0, 0xab, 0xcd, 0xef);
-        libvideo_draw_rectangle(x-10+dx, y-10+dy, 20, 20);
-
-        dx <<= 1;
-        dy <<= 1;
-        libvideo_change_color(0, 0, 255, 255);
-        libvideo_draw_rectangle(x+dx, y+dy, 3, 3);
-
-//        libvideo_change_target(front);
-//        libvideo_draw_bitmap(back, 0,0 );
-        libvideo_do_command_buffer();
-        libvideo_flip_buffers(front);
-
-        frames = frames + 1;
-        uint64_t now = get_tick_count();
-        uint64_t diff = now - start;
-        // In Sek
-        diff /= 1000000;
-        if (diff > oldsec) {
-            oldsec = diff;
-            int fps = frames / diff;
-            printf("Sek %d : %d Frames so far, Overall %d FPS\n", diff, fps, frames);
-        }
-    }
     return 0;
-
 }
diff --git a/src/modules/include/kbd.h b/src/modules/include/kbd.h
index 56e13bc0..d51ca0d8 100644
--- a/src/modules/include/kbd.h
+++ b/src/modules/include/kbd.h
@@ -37,6 +37,8 @@
  * Es handelt sich dabei aber nur um diese Keycodes, die auf allen Layouts am
  * selben Ort sind.
  */
+#define KEYCODE_ESC             1
+#define KEYCODE_ENTER           28
 
 #define KEYCODE_SHIFT_LEFT      42
 #define KEYCODE_SHIFT_RIGHT     54
-- 
2.29.2

