Skip to content

Instantly share code, notes, and snippets.

@wbbradley
Forked from chriscamacho/compo.c
Created April 14, 2020 23:02
Show Gist options
  • Save wbbradley/da0e7413c58dc77e46ee2ecacbb1fe6f to your computer and use it in GitHub Desktop.
Save wbbradley/da0e7413c58dc77e46ee2ecacbb1fe6f to your computer and use it in GitHub Desktop.

Revisions

  1. @chriscamacho chriscamacho revised this gist Apr 14, 2020. 1 changed file with 29 additions and 44 deletions.
    73 changes: 29 additions & 44 deletions compo.c
    Original file line number Diff line number Diff line change
    @@ -54,13 +54,13 @@ int main(void)
    SetTraceLogLevel(LOG_WARNING);
    SetConfigFlags(FLAG_VSYNC_HINT);
    InitWindow(windowWidth, windowHeight, "my 32x32 game/demo");

    // Render texture initialization, used to hold the rendering
    RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
    SetTextureFilter(target.texture, FILTER_POINT); // Texture scale filter to use!
    SetTextureFilter(target.texture, FILTER_POINT); // Texture scale filter to use!

    // ------------ YOUR CODE here -------------
    // NOTE: You don't need to use 3D! (no extra points!)
    // NOTE: You don't need to use 3D! (no extra points!)
    Camera camera = { 0 };
    camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
    @@ -71,78 +71,64 @@ int main(void)
    // Generate torus mesh
    Mesh mesh = GenMeshTorus(0.5f, 1.0f, 16, 32);
    Model model = LoadModelFromMesh(mesh);
    // Checked texture for the torus

    // Checked texture for the torus
    Image imChecked = GenImageChecked(16, 16, 4, 4, RED, WHITE);
    Texture texChecked = LoadTextureFromImage(imChecked);
    UnloadImage(imChecked);

    // Assign generated checked texture to model texture
    model.materials[0].maps[MAP_DIFFUSE].texture = texChecked;
    // ------------ end of YOUR CODE ------------------

    // This texture is used to make a grid mask so you can see the individual pixels
    Image imGrid = GenImageColor(256, 256, BLACK);

    for (int y = 0; y<256; y+=8) {
    for (int x = 0; x<256; x+=8) {
    for (int l=0; l<6; l++) {
    ImageDrawLine(&imGrid, x+1, y+1+l, x+6, y+1+l, BLANK);
    }
    }
    }

    Texture texGrid = LoadTextureFromImage(imGrid);
    UnloadImage(imGrid);

    int frameCount = 0;
    Vector3 angle = { 0 };

    int frameCount = 0;
    // ------------ end of YOUR CODE ------------------

    SetTargetFPS(60); // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose()) // Detect window close button or ESC key
    {
    // Update
    //----------------------------------------------------------------------------------
    frameCount++;
    angle.x += 0.02; angle.y +=0.01; angle.z -= 0.002;
    model.transform = MatrixRotateXYZ(angle);

    model.transform = MatrixRotateXYZ(angle);

    // Compute required framebuffer scaling
    float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight);
    //----------------------------------------------------------------------------------

    // Draw
    //----------------------------------------------------------------------------------
    BeginDrawing();
    ClearBackground(BLACK);

    // Draw everything in the render texture, note this will not be rendered on screen, yet
    BeginTextureMode(target);
    ClearBackground(BLACK); // Clear render texture background color
    // ------------ YOUR CODE here -------------
    DrawTextEx(GetFontDefault(), "World", (Vector2){ 2, 23 }, GetFontDefault().baseSize, 1, (Color){ 0, 255, 255, 255 });
    BeginMode3D(camera);
    DrawModel(model, (Vector3){ 0, 0, 0 }, 1, WHITE);
    EndMode3D();

    // ------------ YOUR CODE here -------------
    DrawTextEx(GetFontDefault(), "World", (Vector2){ 2, 23 }, GetFontDefault().baseSize, 1, (Color){ 0, 255, 255, 255 });
    BeginMode3D(camera);
    DrawModel(model, (Vector3){ 0, 0, 0 }, 1, WHITE);
    EndMode3D();
    DrawTextEx(GetFontDefault(), "Hello", (Vector2){ 4, 0 }, GetFontDefault().baseSize, 1, (Color){ 0, 255, 255, 255 });
    // ------------ end of YOUR CODE ------------------
    // ------------ end of YOUR CODE ------------------

    EndTextureMode();

    // Draw render texture to window, properly scaled
    DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
    (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5,
    (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);

    // The grid like "stencil" is drawn over the squares to make them look not at all like LEDs!
    DrawTexturePro(texGrid, (Rectangle){ 0.0f, 0.0f, (float)texGrid.width, (float)-texGrid.height },
    (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5,
    (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
    // Draw the grid like "stencil" is drawn over the squares to make them look not at all like LEDs!
    for (int x = 0; x < GetScreenWidth(); x += 16) DrawRectangle(x, 0, 4, GetScreenHeight(), BLACK);
    for (int y = 0; y < GetScreenHeight(); y += 16) DrawRectangle(0, y, GetScreenWidth(), 4, BLACK);

    EndDrawing();
    //--------------------------------------------------------------------------------------
    }
    @@ -151,8 +137,7 @@ int main(void)
    UnloadRenderTexture(target); // Unload render texture

    UnloadTexture(texChecked);
    UnloadTexture(texGrid);
    UnloadModel(model);
    UnloadModel(model);

    CloseWindow(); // Close window and OpenGL context
    //--------------------------------------------------------------------------------------
  2. @chriscamacho chriscamacho created this gist Apr 14, 2020.
    160 changes: 160 additions & 0 deletions compo.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,160 @@
    /**********************************************************************************************
    *
    * raylib 32x32 game/demo competition
    *
    * Competition consist in developing a videogame in a 32x32 pixels screen size.
    *
    * RULES:
    *
    * 1) Use only raylib (and included libraries), no external libraries allowed
    * 2) The submission should consist of just one source file
    * 3) Render your game/demo to a 32x32 pixels render texture,
    * show what you could do with a 32x32 RGB LED matrix!
    * 4) No external resources, you CAN only create them programmatically,
    * 5) Game/demo can be 2D or 3D, choose wisely
    * 5) Shaders (if used) should be included in the source as string (char *)
    * and loaded with LoadShaderCode()
    * 6) Code must compile and execute in PLATFORM_DESKTOP (Windows, Linux, macOS)
    *
    *
    * LICENSE: zlib/libpng
    *
    * Copyright (c) 2020 <TODO: participant_name> (<TODO: participant_github_name>)
    *
    * This software is provided "as-is", without any express or implied warranty. In no event
    * will the authors be held liable for any damages arising from the use of this software.
    *
    * Permission is granted to anyone to use this software for any purpose, including commercial
    * applications, and to alter it and redistribute it freely, subject to the following restrictions:
    *
    * 1. The origin of this software must not be misrepresented; you must not claim that you
    * wrote the original software. If you use this software in a product, an acknowledgment
    * in the product documentation would be appreciated but is not required.
    *
    * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
    * as being the original software.
    *
    * 3. This notice may not be removed or altered from any source distribution.
    *
    **********************************************************************************************/

    #include "raylib.h"
    #include "raymath.h"

    #define max(a, b) ((a)>(b)? (a) : (b))
    #define min(a, b) ((a)<(b)? (a) : (b))

    const int windowWidth = 512; // NOTE: Game will be scaled x16
    const int windowHeight = 512;
    const int gameScreenWidth = 32;
    const int gameScreenHeight = 32;

    int main(void)
    {
    SetTraceLogLevel(LOG_WARNING);
    SetConfigFlags(FLAG_VSYNC_HINT);
    InitWindow(windowWidth, windowHeight, "my 32x32 game/demo");

    // Render texture initialization, used to hold the rendering
    RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
    SetTextureFilter(target.texture, FILTER_POINT); // Texture scale filter to use!

    // ------------ YOUR CODE here -------------
    // NOTE: You don't need to use 3D! (no extra points!)
    Camera camera = { 0 };
    camera.position = (Vector3){ 0.0f, 1.0f, 2.0f };
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
    camera.fovy = 45.0f;
    camera.type = CAMERA_PERSPECTIVE;

    // Generate torus mesh
    Mesh mesh = GenMeshTorus(0.5f, 1.0f, 16, 32);
    Model model = LoadModelFromMesh(mesh);

    // Checked texture for the torus
    Image imChecked = GenImageChecked(16, 16, 4, 4, RED, WHITE);
    Texture texChecked = LoadTextureFromImage(imChecked);
    UnloadImage(imChecked);

    // Assign generated checked texture to model texture
    model.materials[0].maps[MAP_DIFFUSE].texture = texChecked;
    // ------------ end of YOUR CODE ------------------

    // This texture is used to make a grid mask so you can see the individual pixels
    Image imGrid = GenImageColor(256, 256, BLACK);

    for (int y = 0; y<256; y+=8) {
    for (int x = 0; x<256; x+=8) {
    for (int l=0; l<6; l++) {
    ImageDrawLine(&imGrid, x+1, y+1+l, x+6, y+1+l, BLANK);
    }
    }
    }

    Texture texGrid = LoadTextureFromImage(imGrid);
    UnloadImage(imGrid);

    int frameCount = 0;
    Vector3 angle = { 0 };

    SetTargetFPS(60); // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose()) // Detect window close button or ESC key
    {
    // Update
    //----------------------------------------------------------------------------------
    frameCount++;
    angle.x += 0.02; angle.y +=0.01; angle.z -= 0.002;

    model.transform = MatrixRotateXYZ(angle);

    // Compute required framebuffer scaling
    float scale = min((float)GetScreenWidth()/gameScreenWidth, (float)GetScreenHeight()/gameScreenHeight);
    //----------------------------------------------------------------------------------

    // Draw
    //----------------------------------------------------------------------------------
    BeginDrawing();
    ClearBackground(BLACK);

    // Draw everything in the render texture, note this will not be rendered on screen, yet
    BeginTextureMode(target);
    ClearBackground(BLACK); // Clear render texture background color

    // ------------ YOUR CODE here -------------
    DrawTextEx(GetFontDefault(), "World", (Vector2){ 2, 23 }, GetFontDefault().baseSize, 1, (Color){ 0, 255, 255, 255 });
    BeginMode3D(camera);
    DrawModel(model, (Vector3){ 0, 0, 0 }, 1, WHITE);
    EndMode3D();
    DrawTextEx(GetFontDefault(), "Hello", (Vector2){ 4, 0 }, GetFontDefault().baseSize, 1, (Color){ 0, 255, 255, 255 });
    // ------------ end of YOUR CODE ------------------

    EndTextureMode();

    // Draw render texture to window, properly scaled
    DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height },
    (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5,
    (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);

    // The grid like "stencil" is drawn over the squares to make them look not at all like LEDs!
    DrawTexturePro(texGrid, (Rectangle){ 0.0f, 0.0f, (float)texGrid.width, (float)-texGrid.height },
    (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5,
    (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE);
    EndDrawing();
    //--------------------------------------------------------------------------------------
    }
    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadRenderTexture(target); // Unload render texture

    UnloadTexture(texChecked);
    UnloadTexture(texGrid);
    UnloadModel(model);

    CloseWindow(); // Close window and OpenGL context
    //--------------------------------------------------------------------------------------
    return 0;
    }