Skip to content

Commit

Permalink
tiny win32 example
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 7, 2024
1 parent d7d5bb1 commit a92d85e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
6 changes: 3 additions & 3 deletions examples/windows/build.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ compile = Rule(

link = Rule(
desc = "Link {files_in} -> {files_out}",
command = "link.exe /nologo {files_in} /out:{files_out} > NUL",
command = "link.exe /nologo {libs} {files_in} /out:{files_out} > NUL",
libs = "user32.lib"
)

def c_binary(files_in, files_out, **kwargs):
objs = [compile(file, **kwargs) for file in files_in]
return link(objs, files_out, **kwargs)


main_o = compile("src/main.cpp")
link([ain_o, , "app")
link(main_o, "app.exe")
45 changes: 40 additions & 5 deletions examples/windows/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
// src/main.cpp
#include "main.hpp"
#include <stdio.h>
#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
DrawText(hdc, "Hello World", 11, &ps.rcPaint, 0);
EndPaint(hwnd, &ps);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR /*pCmdLine*/, int nCmdShow) {
WNDCLASS wc = { };

wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "Sample Window Class";

RegisterClass(&wc);

HWND hwnd = CreateWindowEx(0, "Sample Window Class", "Win32 Hello World",
WS_OVERLAPPEDWINDOW, 400, 400, 400, 400,
NULL, NULL, hInstance, NULL);

ShowWindow(hwnd, nCmdShow);

MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

int main(int argc, char** argv) {
printf("Hello World\n");
return 0;
}
10 changes: 2 additions & 8 deletions tutorial/rules.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

import os

compile_lin = "g++ -MMD -c {files_in} -o {files_out}"
compile_win = "cl.exe /nologo /c {files_in} /sourceDependencies {depfile} /Fo:{files_out} > NUL"

link_lin = "g++ {files_in} -o {files_out}"
link_win = "link.exe /nologo {files_in} /out:{files_out} > NUL"

compile = Rule(
desc = "Compile {files_in} -> {files_out}",
command = compile_win if os.name == 'nt' else compile_lin,
command = "g++ -MMD -c {files_in} -o {files_out}",
files_out = "{swap_ext(files_in, '.o')}",
depfile = "{swap_ext(files_out, '.d')}",
)

link = Rule(
desc = "Link {files_in} -> {files_out}",
command = link_win if os.name == 'nt' else link_lin,
command = "g++ {files_in} -o {files_out}",
)

def c_binary(files_in, files_out, **kwargs):
Expand Down
11 changes: 3 additions & 8 deletions tutorial/tut0.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import os

if os.name == 'nt':
rule = Rule(
command = "cl.exe /c {files_in} /Fo:{files_out}",
)
else:
rule = Rule(
command = "g++ {files_in} -o {files_out}",
)
rule = Rule(
command = "g++ {files_in} -o {files_out}",
)

rule(["src/main.cpp", "src/util.cpp"], "tut0/app")

0 comments on commit a92d85e

Please sign in to comment.