www.mtkn.jp

Manuscripts for my personal webpage.
git clone https://git.mtkn.jp/www.mtkn.jp
Log | Files | Refs | README

commit 5266dedc4286417869f387d4235194a8878625e7
parent cc94ab7aa38bbc698aaa552d23037267ab54891b
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Mon,  2 Jan 2023 10:05:44 +0900

add xlib_playground3

Diffstat:
Mdata/weblog | 19+++++++++++++++++++
Mman/computer/index.html | 1+
Aman/computer/videos/ex3.webm | 0
Mman/computer/xlib_playground2.html | 3+++
Aman/computer/xlib_playground3.html | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mman/index.html | 1+
Mpub/computer/index.html | 1+
Apub/computer/videos/ex3.webm | 0
Mpub/computer/xlib_playground2.html | 2++
Apub/computer/xlib_playground3.html | 192+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpub/index.html | 1+
Mpub/rss.xml | 194++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mpub/sitemap.xml | 7++++---
13 files changed, 579 insertions(+), 15 deletions(-)

diff --git a/data/weblog b/data/weblog @@ -86,3 +86,22 @@ 1672153200 /journal/posts/20221228.html 1672153200 /index.html 1672153200 /journal/index.html +1672585200 /computer/xlib_playground3.html +1703689200 /index.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/index.html +1672585200 /computer/xlib_playground3.html +1672585200 /index.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground2.html +1672585200 /computer/xlib_playground3.html +1672585200 /computer/xlib_playground2.html diff --git a/man/computer/index.html b/man/computer/index.html @@ -6,6 +6,7 @@ Small is beautiful. <ul> <li><a href="xlib_playground1.html">Xlibで遊んでみる1</a></li> <li><a href="xlib_playground2.html">Xlibで遊んでみる2</a></li> + <li><a href="xlib_playground3.html">Xlibで遊んでみる3</a></li> </ul> <h2>MISC</h1> diff --git a/man/computer/videos/ex3.webm b/man/computer/videos/ex3.webm Binary files differ. diff --git a/man/computer/xlib_playground2.html b/man/computer/xlib_playground2.html @@ -224,3 +224,6 @@ main(void) <ul> <li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> </ul> +<p>次の記事: \ +<a href="xlib_playground3.html">Xlibで遊んでみる3</a> +</p> diff --git a/man/computer/xlib_playground3.html b/man/computer/xlib_playground3.html @@ -0,0 +1,173 @@ +<h1>Xlibで遊んでみる3</h1> +<time>2023-01-02</time> + +<p> +前回: <a href="xlib_playground2.html">Xlibで遊んでみる2</a> +</p> +<p> +言語: C言語<br /> +ソースコード: \ +<a href="https://git.mtkn.jp/xlib_playground/log.html">git</a> +</p> + +<h2>画面サイズの変更</h2> +<p> +画面サイズが変更された時に表示している四角形が画面の外側に\ +出ないようにした。<code>XGetWindowAttributes()</code>で\ +画面の情報を取得し、グローバル変数の<code>win_width</code>\ +と<code>win_height</code>に幅と高さをそれぞれ代入して\ +<code>next_tick()</code>で四角形の位置を画面に収まるよう\ +にしている: +</p> +<pre><code>\ +int win_width, win_height; + +void +receive_events(int key_state[]) +{ + XEvent event; + XWindowAttributes wattr; + + while (XPending(display) > 0) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XGetWindowAttributes(display, window, &wattr); + win_width = wattr.width; + win_height = wattr.height; + } break; + /* ... */ + } + } +} + +void +next_tick(long ndt) // nano second +{ + px = px + vx * ndt / 1000 / 1000 / 1000; + py = py + vy * ndt / 1000 / 1000 / 1000; + // bind within the window + if (px < 0) + px = 0; + if (win_width < px + width) + px = win_width - width; + if (py < 0) + py = 0; + if (win_height < py + height) + py = win_height - height; +} +</code></pre> + +<h2>メニュー画面の実装</h2> +<p> +ゲームのようなものを作るうえでメニュー画面とその推移が必要である。\ +ここではグローバル変数<code>next_menu</code>に現在のメニューを保存\ +することにした。それぞれのメニューはそれぞれ関数として記述し、\ +他のメニューに推移する必要が生じたときに<code>next_menu</code>\ +を変更するようにした: +</p> +<pre><code>\ +enum next_menu { + START_MENU, + GAME_PLAY, + GAME_OVER, + QUIT, +}; + +int next_menu = START_MENU; + +void +start_menu(void) +{ + XEvent event; + char *menu_char_q = "press q to quit."; + char *menu_char_s = "press <space> to start."; + + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + while (next_menu == START_MENU) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, + win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, + win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + } break; + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)) { + case 'q': + next_menu = QUIT; + break; + case ' ': + next_menu = GAME_PLAY; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + next_menu = QUIT; + } + } break; + default: + break; + } + } +} + +int +main(void) +{ + setup(); + while (next_menu != QUIT){ + switch (next_menu){ + case START_MENU: + start_menu(); + break; + case GAME_PLAY: + game_play(); + break; + case GAME_OVER: + game_over(); + break; + default: + break; + } + } + + cleanup(); + return 0; +} +</code></pre> +<p><code>main()</code>関数がめっちゃすっきりした。</p> + +<h2>完成品</h2> +<p> +<a href="https://git.mtkn.jp/xlib_playground/file/ex2/ex2.c.html">git</a> +</p> +<p> +<video controls> +<source src="videos/ex3.webm" type="video/webm"> +</video> +</p> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +</ul> diff --git a/man/index.html b/man/index.html @@ -9,6 +9,7 @@ <h2>更新履歴</h2> <a href="/rss.xml">RSS</a> <ul> +<li>2023-01-02 <a href="/computer/xlib_playground3.html">Xlibで遊んでみる3</a></li> <li>2022-12-28 <a href="/journal/posts/20221228.html">Holy Shit!</a></li> <li>2022-12-22 <a href="/computer/xlib_playground2.html">Xlibで遊んでみる2</a></li> <li>2022-12-21 <a href="/computer/xlib_playground1.html">Xlibで遊んでみる1</a></li> diff --git a/pub/computer/index.html b/pub/computer/index.html @@ -29,6 +29,7 @@ Small is beautiful. <ul> <li><a href="xlib_playground1.html">Xlibで遊んでみる1</a></li> <li><a href="xlib_playground2.html">Xlibで遊んでみる2</a></li> + <li><a href="xlib_playground3.html">Xlibで遊んでみる3</a></li> </ul> <h2>MISC</h1> diff --git a/pub/computer/videos/ex3.webm b/pub/computer/videos/ex3.webm Binary files differ. diff --git a/pub/computer/xlib_playground2.html b/pub/computer/xlib_playground2.html @@ -220,6 +220,8 @@ main(void) <ul> <li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> </ul> +<p>次の記事: <a href="xlib_playground3.html">Xlibで遊んでみる3</a> +</p> </article> </main> diff --git a/pub/computer/xlib_playground3.html b/pub/computer/xlib_playground3.html @@ -0,0 +1,192 @@ +<!DOCTYPE html> +<html lang="ja"> +<head> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width,initial-scale=1" /> + <link rel="stylesheet" type="text/css" href="/style.css" /> + <link rel="icon" type="image/x-icon" href="/pics/favicon.ico" /> + <title>Xlibで遊んでみる3</title> +</head> +<body> + <header> + <a href="/">主頁</a> | + <a href="/about.html">自己紹介</a> | + <a href="/journal">日記</a> | + <a href="/farm">農業</a> | + <a href="/kitchen">台所</a> | + <a href="/computer">コンピュータ</a> | + <a href="/poetry">詩</a> | + <a href="/books">本棚</a> | + <a href="https://git.mtkn.jp">Git</a> + </header> + <main> + <article> +<h1>Xlibで遊んでみる3</h1> +<time>2023-01-02</time> + +<p> +前回: <a href="xlib_playground2.html">Xlibで遊んでみる2</a> +</p> +<p> +言語: C言語<br /> +ソースコード: <a href="https://git.mtkn.jp/xlib_playground/log.html">git</a> +</p> + +<h2>画面サイズの変更</h2> +<p> +画面サイズが変更された時に表示している四角形が画面の外側に出ないようにした。<code>XGetWindowAttributes()</code>で画面の情報を取得し、グローバル変数の<code>win_width</code>と<code>win_height</code>に幅と高さをそれぞれ代入して<code>next_tick()</code>で四角形の位置を画面に収まるようにしている: +</p> +<pre><code>int win_width, win_height; + +void +receive_events(int key_state[]) +{ + XEvent event; + XWindowAttributes wattr; + + while (XPending(display) > 0) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XGetWindowAttributes(display, window, &wattr); + win_width = wattr.width; + win_height = wattr.height; + } break; + /* ... */ + } + } +} + +void +next_tick(long ndt) // nano second +{ + px = px + vx * ndt / 1000 / 1000 / 1000; + py = py + vy * ndt / 1000 / 1000 / 1000; + // bind within the window + if (px < 0) + px = 0; + if (win_width < px + width) + px = win_width - width; + if (py < 0) + py = 0; + if (win_height < py + height) + py = win_height - height; +} +</code></pre> + +<h2>メニュー画面の実装</h2> +<p> +ゲームのようなものを作るうえでメニュー画面とその推移が必要である。ここではグローバル変数<code>next_menu</code>に現在のメニューを保存することにした。それぞれのメニューはそれぞれ関数として記述し、他のメニューに推移する必要が生じたときに<code>next_menu</code>を変更するようにした: +</p> +<pre><code>enum next_menu { + START_MENU, + GAME_PLAY, + GAME_OVER, + QUIT, +}; + +int next_menu = START_MENU; + +void +start_menu(void) +{ + XEvent event; + char *menu_char_q = "press q to quit."; + char *menu_char_s = "press <space> to start."; + + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + while (next_menu == START_MENU) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, + win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, + win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + } break; + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)) { + case 'q': + next_menu = QUIT; + break; + case ' ': + next_menu = GAME_PLAY; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + next_menu = QUIT; + } + } break; + default: + break; + } + } +} + +int +main(void) +{ + setup(); + while (next_menu != QUIT){ + switch (next_menu){ + case START_MENU: + start_menu(); + break; + case GAME_PLAY: + game_play(); + break; + case GAME_OVER: + game_over(); + break; + default: + break; + } + } + + cleanup(); + return 0; +} +</code></pre> +<p><code>main()</code>関数がめっちゃすっきりした。</p> + +<h2>完成品</h2> +<p> +<a href="https://git.mtkn.jp/xlib_playground/file/ex2/ex2.c.html">git</a> +</p> +<p> +<video controls> +<source src="videos/ex3.webm" type="video/webm"> +</video> +</p> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +</ul> + </article> + + </main> + <footer> + <address>info(at)mtkn(dot)jp</address> + </footer> +</body> +</html> diff --git a/pub/index.html b/pub/index.html @@ -32,6 +32,7 @@ <h2>更新履歴</h2> <a href="/rss.xml">RSS</a> <ul> +<li>2023-01-02 <a href="/computer/xlib_playground3.html">Xlibで遊んでみる3</a></li> <li>2022-12-28 <a href="/journal/posts/20221228.html">Holy Shit!</a></li> <li>2022-12-22 <a href="/computer/xlib_playground2.html">Xlibで遊んでみる2</a></li> <li>2022-12-21 <a href="/computer/xlib_playground1.html">Xlibで遊んでみる1</a></li> diff --git a/pub/rss.xml b/pub/rss.xml @@ -5,32 +5,182 @@ <description>ウェブページの更新履歴</description> <language>ja-jp</language> <link>https://www.mtkn.jp</link> -<lastBuildDate>Wed, 28 Dec 2022 17:47:41 +0900</lastBuildDate> -<pubDate>Wed, 28 Dec 2022 17:47:41 +0900</pubDate> +<lastBuildDate>Mon, 2 Jan 2023 10:04:45 +0900</lastBuildDate> +<pubDate>Mon, 2 Jan 2023 10:04:45 +0900</pubDate> <docs>https://www.rssboard.org/rss-specification</docs> <item> -<title>Holy Shit!</title> -<link>https://www.mtkn.jp/journal/posts/20221228.html</link> -<guid>https://www.mtkn.jp/journal/posts/20221228.html</guid> -<pubDate>Wed, 28 Dec 2022 00:00:00 +0900</pubDate> -<description><![CDATA[<h1>Holy Shit!</h1> -<p>最近見ているプログラマの配信者がよく言っている。「Holy Shit!」Holyは聖なる、Shitはうんこという意味である。日本のプログラマもうんこという言葉を好むが、海外でも同じようである。 +<title>Xlibで遊んでみる3</title> +<link>https://www.mtkn.jp/computer/xlib_playground3.html</link> +<guid>https://www.mtkn.jp/computer/xlib_playground3.html</guid> +<pubDate>Mon, 2 Jan 2023 00:00:00 +0900</pubDate> +<description><![CDATA[<h1>Xlibで遊んでみる3</h1> +<time>2023-01-02</time> + +<p> +前回: <a href="xlib_playground2.html">Xlibで遊んでみる2</a> </p> <p> -ところでこの言葉の訳語はなにがいいかと考えていると思いがけずぴったりなものを思いついた:</p> +言語: C言語<br /> +ソースコード: <a href="https://git.mtkn.jp/xlib_playground/log.html">git</a> +</p> + +<h2>画面サイズの変更</h2> <p> -「すめらうんこ」 +画面サイズが変更された時に表示している四角形が画面の外側に出ないようにした。<code>XGetWindowAttributes()</code>で画面の情報を取得し、グローバル変数の<code>win_width</code>と<code>win_height</code>に幅と高さをそれぞれ代入して<code>next_tick()</code>で四角形の位置を画面に収まるようにしている: </p> +<pre><code>int win_width, win_height; + +void +receive_events(int key_state[]) +{ + XEvent event; + XWindowAttributes wattr; + + while (XPending(display) > 0) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XGetWindowAttributes(display, window, &wattr); + win_width = wattr.width; + win_height = wattr.height; + } break; + /* ... */ + } + } +} + +void +next_tick(long ndt) // nano second +{ + px = px + vx * ndt / 1000 / 1000 / 1000; + py = py + vy * ndt / 1000 / 1000 / 1000; + // bind within the window + if (px < 0) + px = 0; + if (win_width < px + width) + px = win_width - width; + if (py < 0) + py = 0; + if (win_height < py + height) + py = win_height - height; +} +</code></pre> + +<h2>メニュー画面の実装</h2> <p> -そうそれは、すめらみことの落しもの(droppings)。嗚呼不敬。 +ゲームのようなものを作るうえでメニュー画面とその推移が必要である。ここではグローバル変数<code>next_menu</code>に現在のメニューを保存することにした。それぞれのメニューはそれぞれ関数として記述し、他のメニューに推移する必要が生じたときに<code>next_menu</code>を変更するようにした: +</p> +<pre><code>enum next_menu { + START_MENU, + GAME_PLAY, + GAME_OVER, + QUIT, +}; + +int next_menu = START_MENU; + +void +start_menu(void) +{ + XEvent event; + char *menu_char_q = "press q to quit."; + char *menu_char_s = "press <space> to start."; + + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + while (next_menu == START_MENU) { + XNextEvent(display, &event); + switch (event.type) { + case Expose: { + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_q)/2, + win_height/2, + menu_char_q, strlen(menu_char_q)); + XDrawString(display, window, gc, + win_width/2 - strlen(menu_char_s)/2, + win_height/2 + 20, + menu_char_s, strlen(menu_char_s)); + + } break; + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)) { + case 'q': + next_menu = QUIT; + break; + case ' ': + next_menu = GAME_PLAY; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + next_menu = QUIT; + } + } break; + default: + break; + } + } +} + +int +main(void) +{ + setup(); + while (next_menu != QUIT){ + switch (next_menu){ + case START_MENU: + start_menu(); + break; + case GAME_PLAY: + game_play(); + break; + case GAME_OVER: + game_over(); + break; + default: + break; + } + } + + cleanup(); + return 0; +} +</code></pre> +<p><code>main()</code>関数がめっちゃすっきりした。</p> + +<h2>完成品</h2> +<p> +<a href="https://git.mtkn.jp/xlib_playground/file/ex2/ex2.c.html">git</a> </p> +<p> +<video controls> +<source src="videos/ex3.webm" type="video/webm"> +</video> +</p> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +</ul> ]]></description> </item> <item> <title>Xlibで遊んでみる2</title> <link>https://www.mtkn.jp/computer/xlib_playground2.html</link> <guid>https://www.mtkn.jp/computer/xlib_playground2.html</guid> -<pubDate>Thu, 22 Dec 2022 00:00:00 +0900</pubDate> +<pubDate>Mon, 2 Jan 2023 00:00:00 +0900</pubDate> <description><![CDATA[<h1>Xlibで遊んでみる2</h1> <time>2022-12-22</time> @@ -230,6 +380,26 @@ main(void) <ul> <li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> </ul> +<p>次の記事: <a href="xlib_playground3.html">Xlibで遊んでみる3</a> +</p> +]]></description> +</item> +<item> +<title>Holy Shit!</title> +<link>https://www.mtkn.jp/journal/posts/20221228.html</link> +<guid>https://www.mtkn.jp/journal/posts/20221228.html</guid> +<pubDate>Wed, 28 Dec 2022 00:00:00 +0900</pubDate> +<description><![CDATA[<h1>Holy Shit!</h1> +<p>最近見ているプログラマの配信者がよく言っている。「Holy Shit!」Holyは聖なる、Shitはうんこという意味である。日本のプログラマもうんこという言葉を好むが、海外でも同じようである。 +</p> +<p> +ところでこの言葉の訳語はなにがいいかと考えていると思いがけずぴったりなものを思いついた:</p> +<p> +「すめらうんこ」 +</p> +<p> +そうそれは、すめらみことの落しもの(droppings)。嗚呼不敬。 +</p> ]]></description> </item> <item> diff --git a/pub/sitemap.xml b/pub/sitemap.xml @@ -1,11 +1,12 @@ <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>https://www.mtkn.jp/</loc><lastmod>2023-12-28</lastmod></url> +<url><loc>https://www.mtkn.jp/computer/xlib_playground3.html</loc><lastmod>2023-01-02</lastmod></url> +<url><loc>https://www.mtkn.jp/computer/xlib_playground2.html</loc><lastmod>2023-01-02</lastmod></url> +<url><loc>https://www.mtkn.jp/computer/</loc><lastmod>2023-01-02</lastmod></url> <url><loc>https://www.mtkn.jp/journal/posts/20221228.html</loc><lastmod>2022-12-28</lastmod></url> <url><loc>https://www.mtkn.jp/journal/</loc><lastmod>2022-12-28</lastmod></url> -<url><loc>https://www.mtkn.jp/</loc><lastmod>2022-12-28</lastmod></url> -<url><loc>https://www.mtkn.jp/computer/xlib_playground2.html</loc><lastmod>2022-12-22</lastmod></url> <url><loc>https://www.mtkn.jp/computer/xlib_playground1.html</loc><lastmod>2022-12-22</lastmod></url> -<url><loc>https://www.mtkn.jp/computer/</loc><lastmod>2022-12-22</lastmod></url> <url><loc>https://www.mtkn.jp/books/</loc><lastmod>2022-11-23</lastmod></url> <url><loc>https://www.mtkn.jp/books/978-4-06-288451-8.html</loc><lastmod>2022-11-23</lastmod></url> <url><loc>https://www.mtkn.jp/journal/posts/20221031.html</loc><lastmod>2022-10-31</lastmod></url>