mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-07-02 19:13:43 -04:00
Update/merge examples back into main repo (#192)
This commit is contained in:
102
Examples/Textures/ImageDrawing.cs
Normal file
102
Examples/Textures/ImageDrawing.cs
Normal file
@ -0,0 +1,102 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Image loading and drawing on it
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* This example has been created using raylib 1.4 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public class ImageDrawing
|
||||
{
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
|
||||
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
|
||||
Image cat = LoadImage("resources/cat.png");
|
||||
ImageCrop(ref cat, new Rectangle(100, 10, 280, 380));
|
||||
ImageFlipHorizontal(ref cat);
|
||||
ImageResize(ref cat, 150, 200);
|
||||
|
||||
Image parrots = LoadImage("resources/parrots.png");
|
||||
|
||||
// Draw one image over the other with a scaling of 1.5f
|
||||
Rectangle src = new(0, 0, cat.Width, cat.Height);
|
||||
ImageDraw(ref parrots, cat, src, new Rectangle(30, 40, cat.Width * 1.5f, cat.Height * 1.5f), Color.WHITE);
|
||||
ImageCrop(ref parrots, new Rectangle(0, 50, parrots.Width, parrots.Height - 100));
|
||||
|
||||
// Draw on the image with a few image draw methods
|
||||
ImageDrawPixel(ref parrots, 10, 10, Color.RAYWHITE);
|
||||
ImageDrawCircle(ref parrots, 10, 10, 5, Color.RAYWHITE);
|
||||
ImageDrawRectangle(ref parrots, 5, 20, 10, 10, Color.RAYWHITE);
|
||||
|
||||
UnloadImage(cat);
|
||||
|
||||
// Load custom font for frawing on image
|
||||
Font font = LoadFont("resources/fonts/custom_jupiter_crash.png");
|
||||
|
||||
// Draw over image using custom font
|
||||
ImageDrawTextEx(ref parrots, font, "PARROTS & CAT", new Vector2(300, 230), font.BaseSize, -2, Color.WHITE);
|
||||
|
||||
// Unload custom spritefont (already drawn used on image)
|
||||
UnloadFont(font);
|
||||
|
||||
Texture2D texture = LoadTextureFromImage(parrots);
|
||||
UnloadImage(parrots);
|
||||
|
||||
SetTargetFPS(60);
|
||||
//---------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RAYWHITE);
|
||||
|
||||
int x = screenWidth / 2 - texture.Width / 2;
|
||||
int y = screenHeight / 2 - texture.Height / 2;
|
||||
DrawTexture(texture, x, y - 40, Color.WHITE);
|
||||
DrawRectangleLines(x, y - 40, texture.Width, texture.Height, Color.DARKGRAY);
|
||||
|
||||
DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, Color.DARKGRAY);
|
||||
|
||||
string text = "Source images have been cropped, scaled, flipped and copied one over the other.";
|
||||
DrawText(text, 90, 370, 10, Color.DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user