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); Mon, 22 Feb 2021 20:08:59 +0000 (GMT)
Received: from localhost ([172.29.0.7])
	by sv1.famkaufmann.info with LMTP
	id aA8EICIPNGDeNQAALHnFKQ
	(envelope-from <mayerNico@outlook.de>)
	for <mlmmj-tyndur-devel>; Mon, 22 Feb 2021 20:08:02 +0000
Delivered-To: tyndur-devel@tyndur.org
Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-oln040092065085.outbound.protection.outlook.com [40.92.65.85])
	by sv1.famkaufmann.info (OpenSMTPD) with ESMTPS id 3efa3c79 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO)
	for <tyndur-devel@tyndur.org>;
	Mon, 22 Feb 2021 20:08:01 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=MX51DW2Qz3Oz/MxHjxMWlKq2rb8f9+wPUAVmfEKxO6EfcurTMIPXtrXY0nHuyX8Nz/tBvd5PUBZJeRpw0raFsHvkhq4D17RQ/4UEr+Rs8pdPa0iCfT8NIA0TosoQ06wSeym2TIMJjSAugMcq3CuvcNzmen0GTXkNCvFA1eo0tmhcuWNqhWUN54P233Zw6+A3Th8Z21Jhn2n4TKixHFXE1uLRO0gGETzB9wGmJXIOw7YpAwYFynZzuBJvFrZn/0igdF+3MvE2LmUBJHexrN+Ej5PcpA/yWPeApBTNU2Cl0dwxqnUcoZI3dtCrOthCkktlMGoD6x68iVQ9FZzw+svZag==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com;
 s=arcselector9901;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=nYuXCkuoLUTssi3Fh7JKCSuZm9TJyIWzxRtoSG1jOOQ=;
 b=Ta8AELGJhgARLrvcppRbTuRm8Hd3fk1+uWGw9CNBcjEbUrTl2JXVbqYEGdEXSjiPNSwrzfP2V60BFMZd263El4g3QSg5F2+kfV6B9wgryMOqASJSnkob7EPXB+1P0gDJLtouEcjHdo9fVY134+wd1mjzd0PlnfAsGQslsoQVGjPhGaUPFzONxj3o1Qv0O+NpYpAVZYfOJg+Ib5/DcjwzLKIxmTdsegwnUxjOVOM6nzBPhot2ZL6IuRGBDtjFFb03SQT033QOMzBH3lQuM/2bOLMw9bhJ08UqsoDkW6G8pNnCl2oEBQCIXD4z0Ukne3LVo6/MUzAm6EA0ggI3V2rhZw==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
 dkim=none; arc=none
Received: from VE1EUR01FT059.eop-EUR01.prod.protection.outlook.com
 (2a01:111:e400:7e19::51) by
 VE1EUR01HT179.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e19::507)
 with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27; Mon, 22 Feb
 2021 20:08:00 +0000
Received: from AM8PR04MB7266.eurprd04.prod.outlook.com
 (2a01:111:e400:7e19::43) by VE1EUR01FT059.mail.protection.outlook.com
 (2a01:111:e400:7e19::383) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27 via Frontend
 Transport; Mon, 22 Feb 2021 20:08:00 +0000
X-IncomingTopHeaderMarker:
 OriginalChecksum:E477CF64C74A05872B00203A8113024E303BF045A7D487E2B21327F5626769A7;UpperCasedChecksum:598DA1C0D4A28B04D2CE8F48DCE6C52BDDD35F34F399A9F41133F476278B7392;SizeAsReceived:8360;Count:44
Received: from AM8PR04MB7266.eurprd04.prod.outlook.com
 ([fe80::79d9:ef8a:e6bf:ed44]) by AM8PR04MB7266.eurprd04.prod.outlook.com
 ([fe80::79d9:ef8a:e6bf:ed44%6]) with mapi id 15.20.3868.032; Mon, 22 Feb 2021
 20:08:00 +0000
From: Nico Mayer <mayerNico@outlook.de>
To: tyndur-devel@tyndur.org
Subject: [PATCH v3] modules: reparieren von Pong
Date: Mon, 22 Feb 2021 21:07:52 +0100
Message-ID:
 <AM8PR04MB726684384271B566FAC90EDAD5819@AM8PR04MB7266.eurprd04.prod.outlook.com>
X-Mailer: git-send-email 2.29.2
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-TMN: [CurN36Cm1MCWlm9cBBzkSVJZ1rpSOjHm]
X-ClientProxiedBy: AM0PR03CA0082.eurprd03.prod.outlook.com
 (2603:10a6:208:69::23) To AM8PR04MB7266.eurprd04.prod.outlook.com
 (2603:10a6:20b:1d6::20)
X-Microsoft-Original-Message-ID: <20210222200752.5771-1-mayerNico@outlook.de>
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
X-MS-Exchange-MessageSentRepresentingType: 1
Received: from localhost.localdomain (87.165.243.118) by AM0PR03CA0082.eurprd03.prod.outlook.com (2603:10a6:208:69::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27 via Frontend Transport; Mon, 22 Feb 2021 20:07:59 +0000
X-MS-PublicTrafficType: Email
X-IncomingHeaderCount: 44
X-EOPAttributedMessage: 0
X-MS-Office365-Filtering-Correlation-Id: 33a51430-9cb9-4da5-f227-08d8d76d8d1f
X-MS-TrafficTypeDiagnostic: VE1EUR01HT179:
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info:
	3vW+Wi/0saii8AP6jcrnVTiUGoU8rhKlT9IGVp/dM3MP18NwOocH4C8/qhShsxX+A/uW4D7KWNJibF18BiLIzdj8mDgUq+3KhBFUG271O+iiMtz7TFIZHMx7CQQYDbYmq6rSTYkVQRL0bc99eTvYTKg+kwN1wyoAXhhNeSBPbx3XsEAK03bzmDyipRenBKZOpSq83a0p9YlSaV7YMYRiL5mSsVM9nhKWGESifisBLQGxC8x8u+E0xBtd3hjvl8y/Jmz4E5hdtk2m97us5ZSpHuW7d0GMA6z5g/tOfFyUoSXOLI38kEhXlnDuad4R77dBcMUWkidYVRi4xX1LV7qdblRXU+9krN2nifjNVcJiTkaIBKJSHmHLetdlZmQLFMkyTuIm8nZCcgnZdl0+xh2kjA==
X-MS-Exchange-AntiSpam-MessageData:
	vZDjlCrgzV4LT2iJ6Zx8/CNJTEpTNKax+GKAb/L0lKmOCIE8rakQl3L999kLnVoApOAcT8o6MG26vs0w7+iRiQa1TYA6xyoaOvVEzGcw/+WCirn0Gxmsf3YJiBdgUbRxY16m8eScz7VIwRRQG7N89A==
X-OriginatorOrg: outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: 33a51430-9cb9-4da5-f227-08d8d76d8d1f
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2021 20:08:00.0087
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-AuthSource:
	VE1EUR01FT059.eop-EUR01.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: Internet
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg:
	00000000-0000-0000-0000-000000000000
X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1EUR01HT179

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

Signed-off-by: Nico Mayer <mayerNico@outlook.de>
---
 src/modules/c/pong/main.c | 349 ++++++++++++++++++++++++++++++++------
 1 file changed, 294 insertions(+), 55 deletions(-)

diff --git a/src/modules/c/pong/main.c b/src/modules/c/pong/main.c
index 0773dc21..b6205f8a 100644
--- a/src/modules/c/pong/main.c
+++ b/src/modules/c/pong/main.c
@@ -33,15 +33,271 @@
 #include <syscall.h>
 #include <types.h>
 #include <unistd.h>
+#include <init.h>
+#include <kbd.h>
+#include <lostio.h>
 
-int main(int argc, char** argv)
+#ifndef KEYCODE_ARROW_UP
+#   define KEYCODE_ARROW_UP 103
+#endif
+
+#ifndef KEYCODE_ARROW_DOWN
+#   define KEYCODE_ARROW_DOWN 108
+#endif
+
+#ifndef KEYCODE_ENTER
+#   define KEYCODE_ENTER 28
+#endif
+
+#ifndef KEYCODE_ESC
+#   define KEYCODE_ESC 1
+#endif
+
+//------------------------------------------------------------------------------
+// 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;
+
+    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();
+
+}
+
+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 +341,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;
-
 }
-- 
2.29.2

