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

C++ SFML का परिचय

SFML एक toolkit है जो एक window खोलना और इस पर चीज़ें draw करना आसान बनाता है, एक ready-made art table की तरह। यह clicks और key presses भी handle करता है।
Syntax
cpp
sf::RenderWindow window(sf::VideoMode(width, height), "title");

while (window.isOpen()) {
  sf::Event event;
  while (window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) window.close();
  }
  window.clear();
  window.display();
}

एक SFML Window Create करना

SFML (Simple and Fast Multimedia Library) एक clean C++ API के पीछे windowing, graphics, audio, और input handling देती है।

एक window बनाना बस एक size और title के साथ एक sf::RenderWindow construct करने जितना simple है; Ubuntu पर आप libsfml-dev install करने के बाद इसे link करेंगे, और Windows पर आप prebuilt static libraries के against link करेंगे।

उदाहरण: Creating an SFML Window

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::RenderWindow window(sf::VideoMode(800, 600), "My SFML Window");
	while (window.isOpen()) {
		window.clear();
		window.display();
		break; // demo: exit after one frame
	}
	return 0;
}

Event Loop

एक event loop बार-बार pollEvent() call करता है उन चीज़ों की queue खाली करने के लिए जो last check के बाद हुईं -- window का close होना, एक key press, mouse move होना -- ताकि आपका program हर एक पर react कर सके।

इस loop के बिना, एक window open हो जाएगी लेकिन कभी close होने या click होने पर respond नहीं करेगी।

उदाहरण: The Event Loop

cpp
#include <SFML/Graphics.hpp>

// Program execution starts in main()
int main() {
	sf::RenderWindow window(sf::VideoMode(800, 600), "Events");
	sf::Event event;
	// Repeat while window.isOpen() is true
	while (window.isOpen()) {
		// Repeat while window.pollEvent(event) is true
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				window.close();
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

SFML Coordinates

ज़्यादातर 2D graphics libraries की तरह, SFML coordinate origin (0, 0) को window के top-left corner पर रखता है।

Window के overall pixel dimensions RenderWindow constructor को pass किए गए VideoMode object के जरिए set होते हैं, जो उस coordinate space की outer bounds भी define करता है।

उदाहरण: SFML Coordinates

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::VideoMode mode(800, 600); // origin (0,0) is top-left
	sf::RenderWindow window(mode, "Coordinates");
	return 0;
}

Frame Rate Control करना

बिना किसी limiting के, एक render loop CPU allow करने जितनी तेज़ चलेगा, बेवजह power burn करते हुए। setFramerateLimit() control करता है कि SFML प्रति second कितने frames render करता है, जबकि sf::Clock आपको frames के बीच actual elapsed time खुद measure करने देता है finer control के लिए, जैसे delta time compute करना।

उदाहरण: Controlling Frame Rate

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::RenderWindow window(sf::VideoMode(800, 600), "FPS");
	window.setFramerateLimit(60);

	sf::Clock clock;
	sf::Time elapsed = clock.restart();
	return 0;
}

Texturing और Sprites Intro

एक sprite वह तरीका है जिससे SFML screen पर एक image display करता है -- आप पहले disk से image data को एक sf::Texture में load करते हैं, फिर उस texture को एक sf::Sprite object से attach करते हैं, जिसे इसके reference वाले underlying image data से independently positioned, rotated, और scaled किया जा सकता है।

उदाहरण: Texturing and Sprites Intro

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::Texture texture;
	texture.loadFromFile("player.png");
	sf::Sprite sprite;
	sprite.setTexture(texture);
	sprite.setPosition(100, 100);
	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. SFML libraries link करना भूल जाना (उदाहरण के लिए -lsfml-graphics -lsfml-window -lsfml-system), जो undefined reference errors देता है।
  2. window.pollEvent(event) से कभी events poll न करना, इसलिए window respond करना बंद कर देती है और close नहीं हो सकती।
  3. draw करने से पहले window.clear() और बाद में window.display() भूल जाना, इसलिए कुछ appear नहीं होता या old frames रह जाते हैं।
🔒

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.