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, 19 Feb 2021 15:10:41 +0000 (GMT)
Received: from localhost ([172.29.0.7])
	by sv1.famkaufmann.info with LMTP
	id 6K4+JfDUL2ALMgAALHnFKQ
	(envelope-from <mayerNico@outlook.de>)
	for <mlmmj-tyndur-devel>; Fri, 19 Feb 2021 15:10:40 +0000
Delivered-To: tyndur-devel@tyndur.org
Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075030.outbound.protection.outlook.com [40.92.75.30])
	by sv1.famkaufmann.info (OpenSMTPD) with ESMTPS id 7ff3bdf2 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO)
	for <tyndur-devel@tyndur.org>;
	Fri, 19 Feb 2021 15:10:39 +0000 (UTC)
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=iD0uvYOa+SL63Pucw2f5J52vbuD0xwKFK3Kr3tsVG3k0W/xUCCNej8QFSnDHeEjBCnwv279pK6m2CbjC53gb65w4qkHcZM9sFfXwPOmS4y7UtxFF9j/h0eOTKWeIf16hrjUr08AGhDULZPz1i4Duclzhs7Wq9G4SsczURzBKC2hMpdkKjBCC+CXFWHAuau+FbqR1V5kOsPbHCx8IJheE2V1LDyZ93rwV9ImG2YPFFRAZvu05El0MA805oce27W8YJ0lP2yK7MSYtZr6V9vxR6ai+iA6+CLJYI5t4T7KRi/UAhgAoxYhXjjLVlsbR/hOO02yB3zSbWcd+uaPTMmNI2g==
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=UY55A8p/Oqif2I9YW8REXcquTvzW79enoE69Y3jtuJg=;
 b=FD7pjLazJzJwS1Uz2FaRwtplLcCYyIgj0o202B+wA69hQ1w12re6w1OS/FrJZaSQwHRGitF6TJMPBeUC4iFz1Hm7KXuCrk4jZlD5uNsZZruR6PcV/lYgoG64KFCZQDBFPjZSl9EjO9Y4g0n3ccO0DFpVWds5vR+a8LdxQmtpqo+OUQsVjcsslEBFAdLIcotzLqzt5BaWACsdPdlhpBUpgyv0r78+i21tUiwOcGoXM3deHv2VbgUXXbGrj6ILa8q5CEK3sRaLqp3xS11k8YoMxMhQwtnSZeOjfnSVVBsPvfU2wEgtdRimhHRe9joWOC4xvtBcvgIUQ8U/4Dl9G6zBSw==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none;
 dkim=none; arc=none
Received: from HE1EUR04FT051.eop-eur04.prod.protection.outlook.com
 (2a01:111:e400:7e0d::41) by
 HE1EUR04HT222.eop-eur04.prod.protection.outlook.com (2a01:111:e400:7e0d::105)
 with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27; Fri, 19 Feb
 2021 15:10:38 +0000
Received: from AM8PR04MB7266.eurprd04.prod.outlook.com
 (2a01:111:e400:7e0d::4e) by HE1EUR04FT051.mail.protection.outlook.com
 (2a01:111:e400:7e0d::296) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27 via Frontend
 Transport; Fri, 19 Feb 2021 15:10:38 +0000
X-IncomingTopHeaderMarker:
 OriginalChecksum:B9C13EFC9EE727DCF8CCE2EAC278307AD2B80DBF0A5F3FB0BE3635A07BCE1618;UpperCasedChecksum:E1350A0FF4756C8E1244BF996055871C5373FEA470BBD182E49F5BA8B4578947;SizeAsReceived:7307;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.027; Fri, 19 Feb 2021
 15:10:37 +0000
From: Nico Mayer <mayerNico@outlook.de>
To: tyndur-devel@tyndur.org
Subject: [PATCH] modules: reparieren von Pong
Date: Fri, 19 Feb 2021 16:10:31 +0100
Message-ID:
 <AM8PR04MB72666A2C7C129B9E944CFD0AD5849@AM8PR04MB7266.eurprd04.prod.outlook.com>
X-Mailer: git-send-email 2.29.2
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-TMN: [G24aSTos7Zqu+lBzjnhsE+AYhRuETvR7xv/EeNcYJZfS1/+BeFJ1gtZ6DTkV/U57]
X-ClientProxiedBy: AM0PR02CA0184.eurprd02.prod.outlook.com
 (2603:10a6:20b:28e::21) To AM8PR04MB7266.eurprd04.prod.outlook.com
 (2603:10a6:20b:1d6::20)
X-Microsoft-Original-Message-ID:
 <20210219151031.120316-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 (2003:eb:4f24:ab01:f268:b1c2:b5b6:1472) by AM0PR02CA0184.eurprd02.prod.outlook.com (2603:10a6:20b:28e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.29 via Frontend Transport; Fri, 19 Feb 2021 15:10:37 +0000
X-MS-PublicTrafficType: Email
X-IncomingHeaderCount: 44
X-EOPAttributedMessage: 0
X-MS-Office365-Filtering-Correlation-Id: cc4cecf1-7a2b-43f5-e58f-08d8d4e8832d
X-MS-TrafficTypeDiagnostic: HE1EUR04HT222:
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info:
	1KqeU80UTSp/TNGk3EN9gQtCOdbmujWSkBAH7n5Cv7MRhDfQCnmhmlQFZBDSzbvSD6oGTRoEv1psF4weoHUB0Eg6SULBXe3RDeURILa+XoQqkeF3Sx7UgAYJvDsV5iIf/dWdac6hrGuE+pzyJJLFKOVVYYSRdvuAtqMtj+Q+vmi+85HqHxrf3mNRZRQcCegV/wnN6X6JqztUrI/ncA8gIcsfw5RlxHP6PNc816+Kf1W96zfK6p0uijBcvfaKLyc4IVQlzw50VoEIYAKrBUb/N7YPrW2420RZLZP1kBxSeonvmDM0pjFWSktpLorxFPFBCyXNDSFsjRHh4+rarkgdH11yE3bembpKYSB8XpW/GGBS1oyrAKIjreYg/uRFmEfWWEY+9Iz4hdDQtoLWRZs8sg==
X-MS-Exchange-AntiSpam-MessageData:
	uvtuK7yjLVmLmUTYKYrUl/ELDnyP6iz5fWFAoGna4YI4yxXMgILwW+7Wl7nmZMR75mUs4pbgjJKPKvn1Tuxxtbu4RGqpHfrydkSvZUI4CPoxTzpeNd+soJ0wx3cc2oCYi8EN8uzdRWTH//AjHBfaK6/EFahKesO+/05ecL4s/mUZs5uiiz9Kbc8Ns82x9fUtNROrci6MuNP1in7k+rWZIA==
X-OriginatorOrg: outlook.com
X-MS-Exchange-CrossTenant-Network-Message-Id: cc4cecf1-7a2b-43f5-e58f-08d8d4e8832d
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Feb 2021 15:10:37.9134
 (UTC)
X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-AuthSource:
	HE1EUR04FT051.eop-eur04.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: HE1EUR04HT222

+ Mit dem Programm Pong kann man jetzt wirklich Pong spielen!
---
 src/modules/c/pong/main.c | 370 +++++++++++++++++++++++++++++++-------
 1 file changed, 310 insertions(+), 60 deletions(-)

diff --git a/src/modules/c/pong/main.c b/src/modules/c/pong/main.c
index 0773dc21..9ced7990 100644
--- a/src/modules/c/pong/main.c
+++ b/src/modules/c/pong/main.c
@@ -33,15 +33,308 @@
 #include <syscall.h>
 #include <types.h>
 #include <unistd.h>
+#include <kbd.h>
+#include <init.h>
 
-int main(int argc, char** argv)
+
+//----------------------------------------------------------------------------------
+// Types and Structures Definition
+//----------------------------------------------------------------------------------
+
+
+
+
+typedef struct Vector2 {
+	int x;
+	int y;
+} Vector2;
+
+typedef struct Player {
+	Vector2 position;
+	Vector2 size;
+    int life;
+} 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 start;
+	Key pause;
+} Keyboard;
+
+//------------------------------------------------------------------------------------
+// Global Variables Declaration
+//------------------------------------------------------------------------------------
+
+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_life = 5;
+
+static bool game_over = false;
+static bool pause = false;
+
+static Player player = {0};
+static Ball ball = {0};
+static Keyboard keyboard = {0};
+
+static video_bitmap_t *front;
+
+//------------------------------------------------------------------------------------
+// Module Functions Declaration (local)
+//------------------------------------------------------------------------------------
+
+static void rpc_kbd_callback(pid_t pid, uint32_t cid, size_t data_size, void* data);
+
+static bool init_screen(void);
+static bool init_keyboard(void);
+static bool init_game(void);
+
+
+static void update_player(void);
+static void update_ball(void);
+static void run(void);
+static void render(void);
+
+static void update_key(Key* key, bool down);
+static bool is_key_pressed(Key *key);
+
+//------------------------------------------------------------------------------------
+// Funktion Definitions
+//------------------------------------------------------------------------------------
+
+
+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 run()
+{
+	uint64_t time_last_update = get_tick_count();
+	uint64_t delta_time = 0;
+	
+	while (true)
+	{	
+		delta_time += get_tick_count() - time_last_update;
+		time_last_update = get_tick_count();
+		
+		while (delta_time >= time_per_frame)
+		{
+			delta_time -= time_per_frame;
+			update_player();
+			update_ball();
+		}
+		render();
+	}
+}
+
+static void update_player()
+{
+	if (!pause && !game_over)
+	{
+		if (player.life <= 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;
+		}
+		else if (player.position.y <= 0)
+		{
+			player.position.y = 0;
+		}
+	}
+}
+
+static void update_ball()
+{	
+
+	if (!pause && !game_over)
+	{
+		if (is_key_pressed(&keyboard.start) && !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;
+		}
+		
+		// gooaaal!
+		if (ball.position.x <= 0)
+		{
+			ball.position = (Vector2){screen_width/2, screen_height/2};
+			ball.speed = (Vector2){0, 0};
+			ball.active = false;
+			player.life--;
+		}
+		
+		// collision with player
+		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;
+				ball.speed.y = (ball.position.x - (player.position.y + player.size.x/2))/30;
+			}
+			
+		}
+	}
+}
+
+static void render(void)
+{
+	/* Male background */
+    libvideo_change_color(0,0,0,0);
+    libvideo_draw_rectangle(0,0,screen_width, screen_height);
+
+	/* Male Brett */
+    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);
+
+
+    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.life = player_max_life;
+	
+	/* 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 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 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_ALTGR)
+	{
+		update_key(&keyboard.start, !kbd_data[1]);
+	}
+	
+    v();
+}
+	
+
+static bool init_screen()
 {
     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 +378,32 @@ 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;
-
-    int x = 1024/2;
-    int y = 768/2;
-
-    int dx, dy;
-
-    int frames = 0;
-
-    uint64_t start = get_tick_count();
-
-    int oldsec = 0;
-
     libvideo_change_target(front);
+    
+    return true;
+}
 
-    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;
+int main(int argc, char** argv)
+{
 
+	init_screen();
+	init_keyboard();
+	init_game();
+	
+	run();
+	
+    return 0;
 }
-- 
2.29.2

