My First Windowed Program

A little background...
http://cplusplus.com/forum/beginner/41990/

I decided to choose SFML over SDL and I'm loving it so far, it's really fun and easy to work with. I thought I would post my first program ever in SFML to show how easy it is to work with for anyone interested in starting to use windows and graphics in their programs.

God knows I was getting bored out of my mind doing nothing but boring console programs that output text and read input variables! This program is just based off the tutorials on the website and it's just a simple image viewer / mover.

I commented every little bit of this code, but here are the instructions. Console instructions are fairly obvious (yes to show default image with default settings, no to choose your own image and settings). Here are the commands while the image is open...
1.) Arrow keys: move the image
2.) -/+ keys: rotate the iamge
3.) Pgup + the above keys: speed up moving of the image
4.) Left mouse click: drag the image
5.) Escape: close the window

EXE Link: https://rapidshare.com/files/460100957/Debug.rar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string.h>

void DrawMySprite(sf::Image myImage, int r = 0, int g = 0, int b = 0, int a = 125)
{
    //So we don't have to use sf:: before everything
    using namespace sf;

    // Create the main rendering window with desktop resolution
    VideoMode myVideo;
    RenderWindow App(myVideo.GetDesktopMode(), "Image Viewer");

    // Create the sprite image
    Sprite mySprite(myImage);

    // Change its properties
    mySprite.SetColor(Color(0, 255, 255, 128));
    mySprite.SetPosition(200.f, 100.f);
    mySprite.SetScale(2.f, 2.f);

    // Start game loop
    while (App.IsOpened())
    {
        // Process myEvents
        Event myEvent;
        while (App.GetEvent(myEvent))
        {
            // Close window : exit
            if (myEvent.Type == Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();
	float movRate = 100;

	// Change movement speed while holding pgup/pgdwn
	if (App.GetInput().IsKeyDown(Key::PageUp)) movRate += 100;

        // Move the mySprite
        if (App.GetInput().IsKeyDown(Key::Left))  mySprite.Move( -(movRate) * ElapsedTime, 0 );
        if (App.GetInput().IsKeyDown(Key::Right)) mySprite.Move(  movRate * ElapsedTime, 0 );
        if (App.GetInput().IsKeyDown(Key::Up))    mySprite.Move( 0, -(movRate) * ElapsedTime );
        if (App.GetInput().IsKeyDown(Key::Down))  mySprite.Move( 0,  movRate * ElapsedTime );

        // Rotate the mySprite
        if (App.GetInput().IsKeyDown(Key::Add))      mySprite.Rotate( -(movRate) * ElapsedTime);
        if (App.GetInput().IsKeyDown(Key::Subtract)) mySprite.Rotate( movRate * ElapsedTime);

	// Close application
	if (App.GetInput().IsKeyDown(Key::Escape)) App.Close();

	// Image follows mouse while left button is down
	if (App.GetInput().IsMouseButtonDown( Mouse::Left)) mySprite.SetPosition(App.GetInput().GetMouseX() / 2, App.GetInput().GetMouseY() / 2);

        // Clear screen
        App.Clear(Color(r, g, b, a));

        // Display mySprite in our window
        App.Draw(mySprite);

        // Display window contents on screen
        App.Display();
    }
}
int main()
{
	// So we don't have to use std:: before everything
	using namespace std;

	// Set up our variables to pass into DrawMySprite()
	int red, green, blue, alpha;
	char defaultSettings;
	string myFileName;
	sf::Image myImage;

	// Show our inital menu
	cout << "------Default Settings------" << endl;
	cout << "Would you like to skip manual RGBA/File settings (y/n): ";
	cin >> defaultSettings;
	switch(tolower(defaultSettings))
	{
		// Choose manual settings for image viewer
		case 'n':
			cout << "------Background Color------" << endl;
			cout << "Enter amount of red (0-255): ";
			cin >> red;
			cout << "Enter amount of green (0-255): ";
			cin >> green;
			cout << "Enter amount of blue (0-255): ";
			cin >> blue;
			cout << "Enter amount of transparency (0-255): ";
			cin >> alpha;
			cout << endl;
			cout << "------mySprite Filename------" << endl;
			cout << "Enter a filename (myfile.jpg): ";
			cin >> myFileName;
				if (!myImage.LoadFromFile(myFileName))
					return EXIT_FAILURE;
				DrawMySprite(myImage, red, green, blue, alpha);
				break;
		// Use default settings for image viewer
		case 'y':
				if (!myImage.LoadFromFile("crazy-frog.jpg"))
					return EXIT_FAILURE;
				DrawMySprite(myImage);
				break;
		// Handle invalid selection
		default:
			cout << "ERROR! You have selected an invalid choice." << endl;
			break;
	}
	return EXIT_SUCCESS;
}
Last edited on
Topic archived. No new replies allowed.