# Getting Started with NodGL Welcome to NodGL! This guide will teach you how to create graphics programs in ModuOS, even if you're just starting out with C programming. ## What is NodGL? NodGL is ModuOS's graphics library that lets you: - Draw shapes (rectangles, lines, circles) - Display images and sprites - Create games and graphical applications - Use hardware acceleration (GPU) for fast graphics Think of it like a drawing canvas where you can paint pixels, but much faster! ## Your First NodGL Program Let's create a simple program that shows a colorful rectangle on screen: ```c #include "NodGL.h" #include "libc.h" int md_main(long argc, char **argv) { // Step 1: Create the graphics device NodGL_Device device; NodGL_Context ctx; if (NodGL_CreateDevice(NodGL_FEATURE_LEVEL_1_0, &device, &ctx, NULL) != NodGL_OK) { printf("Failed to start graphics!\n"); return 1; } // Step 2: Clear the screen to black NodGL_ClearContext(ctx, NodGL_CLEAR_COLOR, 0xFF000000, 1.0f, 0); // Step 3: Draw a red rectangle NodGL_FillRectContext(ctx, 100, 100, 200, 150, 0xFFFF0000); // Step 4: Show it on screen NodGL_PresentContext(ctx, 1); // Step 5: Wait a bit so you can see it for (volatile int i = 0; i < 5000000; i++); // Step 6: Clean up NodGL_ReleaseDevice(device); return 0; } ``` ### Understanding Colors Colors in NodGL use **ARGB format** (Alpha, Red, Green, Blue): - `0xFFFF0000` = Red (fully opaque) - `0xFF00FF00` = Green - `0xFF0000FF` = Blue - `0xFFFFFFFF` = White - `0xFF000000` = Black - `0x80FF0000` = Semi-transparent red The format is: `0xAARRGGBB` - `AA` = Alpha (transparency): FF = solid, 00 = invisible - `RR` = Red amount (00 to FF) - `GG` = Green amount (00 to FF) - `BB` = Blue amount (00 to FF) You can also use the helper function: ```c uint32_t color = NodGL_ColorARGB(255, 255, 0, 0); // Red ``` ## Animation Loop Most graphics programs run in a loop to create animation: ```c #include "NodGL.h" #include "libc.h" int md_main(long argc, char **argv) { NodGL_Device device; NodGL_Context ctx; NodGL_CreateDevice(NodGL_FEATURE_LEVEL_1_0, &device, &ctx, NULL); int x = 0; // Starting position // Animation loop - runs 200 times for (int frame = 0; frame < 200; frame++) { // Clear screen NodGL_ClearContext(ctx, NodGL_CLEAR_COLOR, 0xFF000000, 1.0f, 0); // Draw moving rectangle NodGL_FillRectContext(ctx, x, 100, 50, 50, 0xFFFF0000); // Show on screen NodGL_PresentContext(ctx, 1); // Move rectangle to the right x += 2; // Small delay for (volatile int i = 0; i < 50000; i++); } NodGL_ReleaseDevice(device); return 0; } ``` ## Getting Screen Size You often need to know how big the screen is: ```c NodGL_Device device; NodGL_Context ctx; uint32_t screen_width, screen_height; NodGL_CreateDevice(NodGL_FEATURE_LEVEL_1_0, &device, &ctx, NULL); NodGL_GetScreenResolution(device, &screen_width, &screen_height); printf("Screen is %dx%d pixels\n", screen_width, screen_height); ``` ## Common Mistakes ### 1. Forgetting to Present ```c // WRONG - You won't see anything! NodGL_FillRectContext(ctx, 0, 0, 100, 100, 0xFFFF0000); // Missing: NodGL_PresentContext(ctx, 1); // CORRECT NodGL_FillRectContext(ctx, 0, 0, 100, 100, 0xFFFF0000); NodGL_PresentContext(ctx, 1); // Now it shows on screen! ``` ### 2. Wrong Color Format ```c // WRONG - Missing alpha channel uint32_t color = 0xFF0000; // This will be very transparent! // CORRECT - Include alpha (FF = fully visible) uint32_t color = 0xFFFF0000; // Fully opaque red ``` ### 3. Not Clearing the Screen ```c // Without clearing, old frames stay visible (can cause weird effects) for (int i = 0; i < 100; i++) { NodGL_ClearContext(ctx, NodGL_CLEAR_COLOR, 0xFF000000, 1.0f, 0); // Clear each frame // ... draw stuff ... NodGL_PresentContext(ctx, 1); } ``` ## Next Steps Now that you know the basics, check out: - [Drawing Shapes](Drawing-Shapes.md) - Learn to draw lines, circles, and more - [Using Textures](Using-Textures.md) - How to load and display images - [Handling Input](Handling-Input.md) - Respond to keyboard and mouse - [Simple Examples](Simple-Examples.md) - Complete small programs to learn from ## Quick Reference ```c // Initialize NodGL_CreateDevice(NodGL_FEATURE_LEVEL_1_0, &device, &ctx, NULL); // Clear screen to color NodGL_ClearContext(ctx, NodGL_CLEAR_COLOR, 0xFF000000, 1.0f, 0); // Draw filled rectangle NodGL_FillRectContext(ctx, x, y, width, height, color); // Draw line NodGL_DrawLineContext(ctx, x0, y0, x1, y1, color, thickness); // Show on screen NodGL_PresentContext(ctx, 1); // Clean up NodGL_ReleaseDevice(device); ``` Happy coding! 🎮