← Back to C++ Course | Chapter 18: Graphics | Lesson 7 of 7

C++ के साथ SDL2

SDL2 windows खोलने, draw करने, और input handle करने का एक toolkit है, अक्सर games के लिए इस्तेमाल होता है, एक ready-made game-making kit की तरह। यह कई तरह के computers पर काम करता है।
Syntax
cpp
#include <SDL2/SDL.h>

SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("title", x, y, width, height, flags);
SDL_DestroyWindow(window);
SDL_Quit();

SDL2 क्या है?

SDL2 (Simple DirectMedia Layer) एक cross-platform library है जो एक machine के graphics hardware, keyboard, mouse, और audio devices तक low-level, direct access देती है, इसे games और emulators दोनों के नीचे एक common choice बनाते हुए।

Ubuntu पर, इसे sudo apt-get install libsdl2-dev से install करें।

उदाहरण: What is SDL2?

cpp
#include <SDL2/SDL.h>

int main() {
	SDL_Init(SDL_INIT_VIDEO); // low-level access to graphics/input/audio
	SDL_Quit();
	return 0;
}

एक SDL2 Window Create करना

SDL_CreateWindow() एक दिए गए title, position, और size के साथ एक actual OS-level window खोलता है, आगे इसमें draw करने के लिए इस्तेमाल होने वाला एक handle return करते हुए।

आपकी बनाई हर window को eventually SDL_DestroyWindow() से release होना ज़रूरी है जब आपका program exit हो, नहीं तो OS resource allocated रहता है।

उदाहरण: Creating an SDL2 Window

cpp
#include <SDL2/SDL.h>

int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window* window = SDL_CreateWindow("My Window", 0, 0, 800, 600, 0);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

SDL2 में Basic Shapes Render करना

SDL2 में drawing सीधे window पर draw करने की बजाय आपकी window से attached एक SDL_Renderer object से होकर जाता है। SDL_RenderFillRect() उस renderer इस्तेमाल करते हुए एक दी गई position और size पर एक solid, color-filled rectangle draw करता है।

उदाहरण: Rendering basic Shapes in SDL2

cpp
#include <SDL2/SDL.h>

int main() {
	SDL_Window* window = SDL_CreateWindow("Shapes", 0, 0, 800, 600, 0);
	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_Rect rect = {50, 50, 120, 80};
	SDL_RenderFillRect(renderer, &rect);
	return 0;
}

Event Handling

एक loop में call किया गया SDL_PollEvent() उन सारी चीज़ों की queue खाली करता है जो OS ने last check के बाद report कीं -- key presses, mouse movement, window-close requests -- आपके program को हर event type पर branch करने और accordingly respond करने देते हुए।

उदाहरण: Event Handling

cpp
#include <SDL2/SDL.h>

// Program execution starts in main()
int main() {
	SDL_Event event;
	// Declare running and set it to true
	bool running = true;
	// Repeat while running && SDL_PollEvent(&event) is true
	while (running && SDL_PollEvent(&event)) {
		if (event.type == SDL_QUIT)
			running = false;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Clearing और Presenting

SDL_RenderClear() अगले frame का content draw करने से पहले renderer के buffer को वापस एक blank state में wipe करता है, और SDL_RenderPresent() फिर उस finished buffer को actual screen पर flip करता है -- present call किए बिना, आपकी कोई भी drawing कभी visible नहीं होगी।

उदाहरण: Clearing and Presenting

cpp
#include <SDL2/SDL.h>

int main() {
	SDL_Window* window = SDL_CreateWindow("Present", 0, 0, 800, 600, 0);
	SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_RenderClear(renderer);
	SDL_RenderPresent(renderer); // flips the buffer onto the screen
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पहले SDL_Init call किए बिना SDL_CreateWindow call करना।
  2. अंत में SDL_Quit() भूल जाना और window और renderer destroy करना, जो resources leak करता है।
  3. main loop में SDL_PollEvent poll न करना, इसलिए window unresponsive हो जाती है।
चैप्टर सारांश
  • C++ graphics SFML, OpenGL, और SDL2 जैसी external libraries इस्तेमाल करता है।
  • SFML आपको shapes draw करने और animations बनाने देता है।
  • Programs interactive बनने के लिए mouse और keyboard input पर respond कर सकते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.