From 6fb035821f4a92f29ab245920d7fb8db69c0c9b9 Mon Sep 17 00:00:00 2001 From: Marina Barros Date: Sat, 7 Nov 2026 13:58:45 -0300 Subject: commit inicial: GLFW e janela vazia --- .envrc | 1 + .gitignore | 1 + README.md | 19 ++++ include/main.h | 15 +++ justfile | 13 +++ npins/default.nix | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++++ npins/sources.json | 12 +++ shell.nix | 18 ++++ src/main.c | 80 ++++++++++++++++ 9 files changed, 436 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 include/main.h create mode 100644 justfile create mode 100644 npins/default.nix create mode 100644 npins/sources.json create mode 100644 shell.nix create mode 100644 src/main.c diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89f9ac0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..12cd19f --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Filtro CRT + +## Ideia + +Inspirado no vídeo [Static, de Flavor Foley ft. Hatsune Miku](https://www.youtube.com/watch?v=KlTNKOnfXFk), a ideia é uma ferramenta que recebe uma imagem arbitrária e aplica um "filtro CRT" nela, saturando as cores e gerando aquele efeito de "sombra" que aparece no vídeo. + +## Como fazer + +- Programa deve receber uma imagem e aplicar um shader + - Olhar shaders do Cathode Retro +- Imagem de saída sai com a saturação do filtro CRT + +## Referências + +- [Cathode Retro](https://www.cathoderetro.com/docs/) + +## Aprendizados + +> It turns out, color can be encoded as a wave with a specific frequency, by encoding the **hue of the color as the wave's phase** (its side-to-side positioning relative to a reference wave) and the **saturation of the color as the wave's amplitude**. Coupled with the existing broadcast's brightness information (now referred to as the luma of the signal), this gives the TV everything it needs to display a full color picture. diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..19fa03c --- /dev/null +++ b/include/main.h @@ -0,0 +1,15 @@ +#include + +typedef struct { + float r; + float g; + float b; +} CRT_RGB; + +void error_callback(int error, const char* description); + +void framebuffer_size_callback(GLFWwindow* window, int width, int height); + +void processInput(GLFWwindow *window); + +void hexToRGB(CRT_RGB* rgb, const char* hex); diff --git a/justfile b/justfile new file mode 100644 index 0000000..9960c58 --- /dev/null +++ b/justfile @@ -0,0 +1,13 @@ +CC := "gcc -Wall -Wextra -Wpedantic -std=c99" +# https://learnopengl.com/Getting-started/Creating-a-window +# For Linux users compiling with GCC, the following command line options may help you compile the project: -lglfw3 -lGL -lX11 -lpthread -lXrandr -lXi -ldl. Not correctly linking the corresponding libraries will generate many undefined reference errors. +# LIBS := `pkg-config --libs glfw3` +LIBS := `pkg-config --libs-only-l glfw3 gl` +INCLUDE := "include" + +build: + @test -d out/ || mkdir -p out/ + {{CC}} -o out/main {{LIBS}} -I{{INCLUDE}} src/main.c + +run: + nixGL out/main diff --git a/npins/default.nix b/npins/default.nix new file mode 100644 index 0000000..f02eb3f --- /dev/null +++ b/npins/default.nix @@ -0,0 +1,277 @@ +/* + This file is provided under the MIT licence: + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ +# Generated by npins. Do not modify; will be overwritten regularly +let + # Backwards-compatibly make something that previously didn't take any arguments take some + # The function must return an attrset, and will unfortunately be eagerly evaluated + # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments + mkFunctor = + fn: + let + e = builtins.tryEval (fn { }); + in + (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; }; + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 + range = + first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1); + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 + stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); + + # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 + stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); + concatStrings = builtins.concatStringsSep ""; + + # If the environment variable NPINS_OVERRIDE_${name} is set, then use + # the path directly as opposed to the fetched source. + # (Taken from Niv for compatibility) + mayOverride = + name: path: + let + envVarName = "NPINS_OVERRIDE_${saneName}"; + saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name; + ersatz = builtins.getEnv envVarName; + in + if ersatz == "" then + path + else + # this turns the string into an actual Nix path (for both absolute and + # relative paths) + builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" ( + if builtins.substring 0 1 ersatz == "/" then + /. + ersatz + else + /. + builtins.getEnv "PWD" + "/${ersatz}" + ); + + mkSource = + name: spec: + { + pkgs ? null, + }: + assert spec ? type; + let + # Unify across builtin and pkgs fetchers. + # `fetchGit` requires a wrapper because of slight API differences. + fetchers = + if pkgs == null then + { + inherit (builtins) fetchTarball; + fetchurl = + { + url, + sha256, + }: + builtins.fetchurl { + name = "source"; + inherit url sha256; + }; + # Frustratingly, due to flakes and `fetchTree`, `fetchGit` + # has a different signature than the other builtin + # fetchers + fetchGit = args: (builtins.fetchGit args).outPath; + } + else + { + fetchTarball = + { + url, + sha256, + }: + pkgs.fetchzip { + inherit url sha256; + extension = "tar"; + }; + fetchurl = + { + url, + sha256, + }: + pkgs.fetchurl { + name = "source"; + inherit url sha256; + }; + fetchGit = + { + url, + submodules, + rev, + name, + narHash, + }: + pkgs.fetchgit { + inherit url rev name; + fetchSubmodules = submodules; + hash = narHash; + }; + }; + + path = + if spec.type == "Git" then + mkGitSource fetchers spec + else if spec.type == "GitRelease" then + mkGitSource fetchers spec + else if spec.type == "PyPi" then + mkPyPiSource fetchers spec + else if spec.type == "Channel" then + mkChannelSource fetchers spec + else if spec.type == "Url" || spec.type == "MutableUrl" then + mkUrlSource fetchers spec + else if spec.type == "Container" then + mkContainerSource pkgs spec + else + builtins.throw "Unknown source type ${spec.type}"; + in + spec // { outPath = mayOverride name path; }; + + mkGitSource = + { + fetchTarball, + fetchGit, + ... + }: + { + repository, + revision, + url ? null, + submodules, + hash, + ... + }: + assert repository ? type; + # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository + # In the latter case, there we will always be an url to the tarball + if url != null && !submodules then + fetchTarball { + inherit url; + sha256 = hash; + } + else + let + url = + if repository.type == "Git" then + repository.url + else if repository.type == "GitHub" then + "https://github.com/${repository.owner}/${repository.repo}.git" + else if repository.type == "GitLab" then + "${repository.server}/${repository.repo_path}.git" + else if repository.type == "Forgejo" then + "${repository.server}/${repository.owner}/${repository.repo}.git" + else + throw "Unrecognized repository type ${repository.type}"; + urlToName = + url: rev: + let + matched = builtins.match "^.*/([^/]*)(\\.git)?$" url; + + short = builtins.substring 0 7 rev; + + appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else ""; + in + "${if matched == null then "source" else builtins.head matched}${appendShort}"; + name = urlToName url revision; + in + fetchGit { + rev = revision; + narHash = hash; + + inherit name submodules url; + }; + + mkPyPiSource = + { fetchurl, ... }: + { + url, + hash, + ... + }: + fetchurl { + inherit url; + sha256 = hash; + }; + + mkChannelSource = + { fetchTarball, ... }: + { + url, + hash, + ... + }: + fetchTarball { + inherit url; + sha256 = hash; + }; + + mkUrlSource = + { + fetchTarball, + fetchurl, + ... + }: + { + url, + hash, + unpack, + ... + }: + (if unpack then fetchTarball else fetchurl) { + inherit url; + sha256 = hash; + }; + + mkContainerSource = + pkgs: + { + image_name, + image_tag, + image_digest, + hash, + ... + }@args: + if pkgs == null then + builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers" + else + pkgs.dockerTools.pullImage ( + { + imageName = image_name; + imageDigest = image_digest; + finalImageTag = image_tag; + hash = hash; + } + // (if args.arch or null != null then { arch = args.arch; } else { }) + ); + +in +mkFunctor ( + { + input ? ./sources.json, + }: + let + data = + if builtins.isPath input then + # while `readFile` will throw an error anyways if the path doesn't exist, + # we still need to check beforehand because *our* error can be caught but not the one from the builtin + # See: + if builtins.pathExists input then + builtins.fromJSON (builtins.readFile input) + else + throw "Input path ${toString input} does not exist" + else if builtins.isAttrs input then + input + else + throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset"; + version = data.version; + in + if version == 8 then + builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins + else + throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`" +) diff --git a/npins/sources.json b/npins/sources.json new file mode 100644 index 0000000..fc665ec --- /dev/null +++ b/npins/sources.json @@ -0,0 +1,12 @@ +{ + "pins": { + "nixpkgs": { + "type": "Channel", + "name": "nixpkgs-unstable", + "artifact": "nixexprs.tar.xz", + "url": "https://releases.nixos.org/nixpkgs/nixpkgs-26.11pre1106208.d357a2b54f0b/nixexprs.tar.xz", + "hash": "sha256-gh035dq/sQ/OwPltGyvjNl1zNWZgVnng2zug7xKqSu8=" + } + }, + "version": 8 +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..b4cae02 --- /dev/null +++ b/shell.nix @@ -0,0 +1,18 @@ +let + sources = import ./npins; + pkgs = import sources.nixpkgs { }; +in +pkgs.mkShell { + packages = with pkgs; [ + just + pkg-config + ]; + + buildInputs = with pkgs; [ + glfw + ]; + + shellHook = '' + export PKG_CONFIG_PATH="${pkgs.glfw.dev}/lib/pkgconfig:$PKG_CONFIG_PATH" + ''; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..1675754 --- /dev/null +++ b/src/main.c @@ -0,0 +1,80 @@ +#include +#include + +#include "main.h" + +int main() +{ + if (!glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) { + printf("Wayland is not supported\n"); + return 1; + } + glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND); + + if (!glfwInit()) { + printf("Failed to initialize GLFW\n"); + return 1; + } + glfwSetErrorCallback(error_callback); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL); + if (window == NULL) { + printf("Failed to create GLFW window\n"); + glfwTerminate(); + return 1; + } + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + glViewport(0, 0, 800, 600); + + while(!glfwWindowShouldClose(window)) { + processInput(window); + + glClearColor(0.121f, 0.157f, 0.246f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + glfwTerminate(); + + return 0; +} + +void error_callback(int error, const char* description) +{ + fprintf(stderr, "Error %d: %s\n", error, description); +} + +void framebuffer_size_callback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} + +void processInput(GLFWwindow *window) +{ + if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, GLFW_TRUE); +} + +/* TODO */ +/* void hexToRGB(CRT_RGB* rgb, const char* hex) { */ +/* if (strlen(hex) == 0) { */ +/* printf("Invalid hex: %s", hex); */ +/* return; */ +/* } */ + +/* if (hex[0] != '#') { */ +/* printf("Invalid hex: %s", hex); */ +/* return; */ +/* } */ + +/* const char* r; */ +/* const char* g; */ +/* const char* b; */ +/* } */ -- cgit v1.2.3