Skip to content

Instantly share code, notes, and snippets.

View nvictor's full-sized avatar

Victor Noagbodji nvictor

View GitHub Profile
@nvictor
nvictor / win32_main.c
Last active February 20, 2020 23:41
Win32 API standard main
#include <windows.h>
// Win32 API programs entry point
// 1. C might give you a hard time if you don't name the parameters
// 2. use __stdcall, which is an alias for WINAPI, which is an alias for APIENTRY, etc...
int __stdcall wWinMain(
// call it module, not instance (back in the days modules share multiple instances, not anymore).
HINSTANCE module,
// previous "instance", not often used
HINSTANCE previous,
@nvictor
nvictor / win32_create_window.c
Last active February 20, 2020 23:42
Win32 API create window
void create_window(HINSTANCE module, LPCTSTR class_name, LPCTSTR title) {
// 1. filling WNDCLASS / WNDCLASSEX
// wc = {} and wc {.hCursor = ..., .hInstance = ...} don't work in VS2017
WNDCLASS wc = {0};
// Only 3 components are necessary
// hCursor sets the cursor for when the mouse is over the window
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
// hInstance is the handle to the module's instance
// and lpszClassName is the window class name.
@nvictor
nvictor / win32_window_proc.c
Last active February 20, 2020 23:43
Win32 API standard window proc
LRESULT __stdcall window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) {
switch(message) {
// calls to BeginPaint and EndPaint are still needed
// even when using newer rendering engines
case WM_PAINT:
{
PAINTSTRUCT ps;
VERIFY(BeginPaint(window, &ps));
EndPaint(window, &ps);
}
@nvictor
nvictor / win32_process_messages.c
Last active February 20, 2020 23:50
Win32 API process messages
// basic version
void process_messages1() {
MSG message;
BOOL result;
// GetMessage() blocks and waits for a message
// returns 0 when it sees WM_QUIT
// returns -1 when something goes wrong
// returns a nonzero when there is a valid message to dispatch
while (result = GetMessage(&message, 0, 0, 0)) {
if (result != -1) {
@nvictor
nvictor / macros.c
Last active February 20, 2020 23:51
C Macros
#define LEN(A) (sizeof(A) / sizeof((A)[0]))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
// MAX3, MAX4, etc could be implented in terms of MAX, i.e. MAX((a), MAX((b), (c)))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
// C static assert
// if expr is true then evaluates sizeof(char[1])
// if expr is false then evaluates sizeof(char[-1]) which is undefined behavior
#define SASSERT(expr) ((void)sizeof(char[1 - 2 * !(expr)]))
@nvictor
nvictor / fatal.c
Last active February 20, 2020 23:38
Fatal and error functions
#include <stdarg.h> # va_list, va_start, va_end
#include <stdio.h> # vprintf
#include <stdlib.h> # exit
void fatal(const char *format, ...) {
va_list args;
va_start(args, format);
printf("Fatal: ");
vprintf(format, args);
printf("\n");
@nvictor
nvictor / youtube_dl_mp3.sh
Last active January 5, 2023 14:09
youtube_dl_mp3
youtube-dl --extract-audio --audio-format=mp3 --output="%(title)s.%(ext)s" --batch-file="mus.txt"
@nvictor
nvictor / youtube_dl_mp4.sh
Last active January 5, 2023 14:09
youtube_dl_mp4
youtube-dl --format=mp4 --output="%(title)s.%(ext)s" --restrict-filenames --batch-file="vid.txt"
@nvictor
nvictor / aria2c_torrent
Created January 13, 2019 16:14
aria2c_torrent.sh
aria2c --input-file=/path/dlist.txt --seed-ratio=0
ffmpeg -i input.mp3 -ss hh:mm:ss.lll -t ddd -acodec copy output.mp3