← Back to C Course | Chapter 13: Graphics | Lesson 9 of 10

C SDL2 परिचय

SDL2 windows खोलने, draw करने, और keys और mouse handle करने के लिए एक toolkit है, अक्सर games के लिए इस्तेमाल होती है। यह एक ready-made game-making kit जैसा है।
Syntax
c
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("title", x, y, width, height, flags);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, flags);
// ... draw with renderer ...
SDL_Quit();

SDL2 क्या है?

SDL2 (Simple DirectMedia Layer) एक cross-platform C library है जो graphics rendering, keyboard और mouse input, और audio तक low-level, hardware-accelerated access देती है, और DOS-era BGI library के उलट, यह actually modern Windows, Linux, और macOS systems पर compile और चलती है।

उदाहरण: What is SDL2?

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Quit();
	return 0;
}

एक Window और Renderer बनाना

SDL2 में कुछ भी draw करना screen पर actual window represent करने के लिए एक SDL_Window बनाने से शुरू होता है, फिर उस window से bound एक paired SDL_Renderer, क्योंकि renderer — window खुद नहीं — actually वह है जिसे आपकी बाद की सारी drawing calls target करती हैं।

उदाहरण: Creating a Window and Renderer

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

SDL2 में Shapes Draw करना

SDL_RenderDrawLine() और SDL_RenderFillRect() SDL2 के दो core drawing functions हैं, आपको respectively दो points के बीच एक line draw करने या renderer पर एक rectangular region भरने देते हैं; drawing calls issue करने के बाद आपको उन्हें actually display करने के लिए SDL_RenderPresent() call करना होगा।

उदाहरण: Drawing Shapes in SDL2

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	SDL_RenderDrawLine(renderer, 0, 0, 100, 100);
	SDL_RenderPresent(renderer);
	SDL_Quit();
	return 0;
}

Basic Keyboard Events Handle करना

SDL2 का अपना कोई automatic event loop नहीं है, इसलिए आपके program को key presses या window-close request जैसे pending input check करने के लिए अपने loop के अंदर बार-बार SDL_PollEvent() call करना होगा, और आपके program को मायने रखने वाले हर event type पर respond करना होगा।

उदाहरण: Handling Basic Keyboard Events

c
#include <SDL2/SDL.h>
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Event event;
	while (SDL_PollEvent(&event)) {
		if (event.type == SDL_KEYDOWN) {
			// key was pressed
		}
	}
	SDL_Quit();
	return 0;
}

Custom Pixel Grids Draw करना

क्योंकि SDL2 आपको इसके renderer के जरिए individual pixels पर सीधा control देता है, points या छोटे filled rectangles की nested loops draw करना आपको custom pixel grids, tile-based maps, या procedurally generated patterns बनाने देता है जिन्हें SDL2 का कोई built-in shape function सीधे produce नहीं करता।

उदाहरण: Drawing Custom Pixel Grids

c
#include <SDL2/SDL.h>
// Program execution starts in main()
int main() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window *window = SDL_CreateWindow("App", 0, 0, 640, 480, 0);
	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
	// Loop: repeat while the condition holds
	for (int x = 0; x < 10; x++) {
		// Loop: repeat while the condition holds
		for (int y = 0; y < 10; y++) {
			SDL_RenderDrawPoint(renderer, x * 5, y * 5);
		}
	}
	SDL_Quit();
	// Return 0 to signal that the program finished successfully
	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. -lSDL2 जैसे flags से SDL2 link करना भूल जाना, जो undefined reference errors cause करता है।
  2. एक window बनाने से पहले SDL_Init call न करना, या खत्म होने पर SDL_Quit call न करना।
  3. SDL_PollEvent से events poll न करना, ताकि window unresponsive हो जाए।

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.