ball wont load?

so i am making pong and my ball wont load and i cant figure out the reason. i got no errors or warning so i have got nothing to work with on this problem!
also the line of code that has a drawline doesnot seem to load either

below is the code i am using


//\====================================================================================
//\ Author: declan.corcoran
//\ Date : 14 March 2012
//\ About : main.cpp - Defines the entry point for our Simple SDL Project
//\====================================================================================

#include <Windows.h>
#include "GL_Functions.h"
#include "pctimer.h"
#include <string>
#include <cmath>
#include <crtdbg.h>


int main(int argc, char* argv[])
{

//Lets open the window and initialise opengl
InitGL(1024,768);


//Loads an Image, we store it in the integer grass

//TEXTURES
int iBackground = LoadTexture("./images/background.png");
int iBallTexture = LoadTexture ("./images/mball.png");
int ibackground = 2;
int iBall = 2;
int iBallx = 492;
int iBally = 384;
//Used as a position for our tile of grass
float x = 0;
float y = 0;

float iBall_Width = 2.5;
int iBall_Length = 5;
int Ballxspeed = 4;
int Ballyspeed = 2;






{
//ball speed/control
if(iBallx > 984)
{
iBallx = 492;
iBally = 384;
Ballxspeed = -1;
}

if ( iBallx < 0)
{
iBallx = 492;
iBally = 384;
Ballxspeed = -1;
}

if (iBally > 1024 || iBally < 0)
Ballyspeed = -.5;
}

do
{
//Clear the screen, so previous frames don't build up
ClearScreen();

//ballspeed
iBallx = iBallx + Ballxspeed;
iBally = iBally + Ballyspeed;

DrawLine(512,0,512,768);
DrawSprite(iBall, x, y, 40, 40);
DrawSprite(ibackground, x, y, 1024, 768);
//Example Text


//Stop it from running too fast! Sleep ZZzzz
Sleep(5);

} while (FrameworkUpdate()); //Do some secret stuff,

//Before you exit, clean up after yourself

FreeTexture(ibackground);
FreeTexture(iBallTexture);
//Close down
CloseDown();

_CrtDumpMemoryLeaks();

//Quit!
return 0;
}

thanks for your time and effort
Is your texture definitely loading in?

Might be worth putting some checking in there to make sure the textures load. That'd be the first thing I'd try. (Been a little while since I worked with SDL).

EDIT: Or perhaps it's the x,y values you're passing into DrawSprite for the ball. Shouldn't they be iBallx and iBally?
Last edited on
I don't know anything about SDL, but you don't seem to be doing anything with the loaded texture.

you have iBallTexture, and iBall, but at no point does iBall = iBallTexture, and you try to draw iBall, which is just the int value 2, unless the int value 2 corresponds to the second texture you've loaded, but that seems pretty silly.

as to drawing it using x, y which are both 0, it would draw it anyway, unless SDL draws sprites based on the position of the bottom right corner, with 0, 0 being the top left corner of the window, but that again seems silly since 0, 0 should always be top left in programming and it would be stupid to have a different coordinate system based on the window and the sprite, although maybe 0, 0 corresponds to the top left corner of the screen, rather than the window, so it isn't visibly drawn.
Last edited on
iball is supposed to contain and initialise iballtexture....but i not very good at this.

and with the positions //Lets open the window and initialise opengl
InitGL(1024,768);
this is supposed to limit it and start at the top corner...but then again i am not very experienced at this
InitGL simply opens the window, like when you open your web browser, a window opens, it doesn't do anything else, the 1024, 768 is the dimensions of the window (width, height)

if you aren't required to use SDL, I recommend switching to SFML, it is easier to use, and if you use version 1.6 there are tutorials for you, if you decide you want to use version 2.0 by compiling it from source, sfmlcoder (xander314 on here, I think) has videos on youtube about getting started (compiling 2.0, linking it with codeblocks/visualstudio, and making your first program with sprites, button presses, etc.)

looking over your code again, it doesn't seem to be doing anything at all, except opening a window and drawing a background.

I'm working on a pong clone myself, spent a few hours on it, it isn't difficult to do at all if you are really dedicated and know the basics of c++.

sfml-dev.org
Last edited on
..nah i have to use SDL...and i dont know the basics particular well as is probable evident.. my course flew over the basics expecting everyone to know it instantly and then dumped us with our first assignment pong and now i cant even keep up with the pace of the course before we even finished our first assignment we have started to learn stuff required for our second assignment

and your doing a pong clone? help me finish mine lol? i also got a new code that i have made that works better but still doesn't work to well do you want me to upload it?
I wouldn't know how to help you if you are using SDL, besides, that is cheating, although it does seem unfair that your class skimmed the basics, then again if it is a college/university class maybe not, if it is a high school class then other people will no doubt be having problems as well, maybe you should help each other out.

there are some good video tutorials for the basics of c++ on youtube, I recommend these 2:

Buckys C++ Tutorials (https://www.youtube.com/watch?v=tvC1WCdV1XU)

and

AntiRTFMs SpoonFed Tutorials (https://www.youtube.com/watch?v=tyVhn0FWWB4)

you can skip ahead if you like, each has 60-70+ videos and the first 20 or so are the real basics, the SpoonFed tutorials are easier and more newbish, but I prefer those.

I'm sure there are also video tutorials on SDL, or failing that, I know there are plenty of text tutorials on SDL.
Last edited on
yea i spent most of my time at uni watching videos trying to catch up...3rd week of uni and we have started with variables,loops flow control..first day and now we covered pointers,arrays,dynamic arrays,macros, logging memory leaks, passing points via pointers,passing by references and consts,string manipulation, multi source files and so the list goes on...its stupid how much we go through and are meant to remember it all like I'm not dumb or anything but this is just crazy >.<

and i didn't want your code just ur help lol =D
and it university but we dont have much time to work on old stuff as new stuff gets chucked at us..they changed the structure of the course 2 weeks before we started it....changed from a 3 year course to a 2 year course...so there cramming everything in...if i had been told that it had been changed i would of pulled out but now i already paided my fees >.< 16grand worth of em and now i'm stuck doing a course that i cant keep up with...but that's my problem =D
Wow, so much money to waste away, I hope the course is worth it, any idea what you are expected to have achieved/know by the end? you could probably have just learned c++ on your own in 2 years and get an equal amount of skill in it, as long as you do it every day and don't stop.

I'm curious though, how often are these classes? I'm going to college soon to start a programming class (even though I doubt I'll learn anything) and I'm wondering if it is a few hours each working day, or an entire day per week (which would make sense considering the HW projects I've seen people post on here)

about me helping you, like I said I don't use SDL, not that I didn't try, but after a few days it was hopeless and I had heard about SFML which was supposedly better, easier and object oriented, I've been using it for a week, and no complaints so far.

this is my current code for SFML, maybe you can pick up on some of the things I do and use it in SDL, but I doubt it will be of much use, this I don't consider cheating, since you still have to figure things out for yourself, I'm just missing a way of making my ball bounce around the screen and at angles, plus some text to display, and it should be finished, but I will get around to get some other day, I've not been feeling well, hence why I've been on here for the last few hours, so I don't fall asleep and wake up at 3 AM >.>

haven't bothered with comments either, it was just a quick test of what I could mash together in SFML, and it somehow turned into ping pong.

looks like copy and pasting it has screwed up some of the indentation, oh well.
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    int Width = 800;
    int Height = 600;
	sf::RenderWindow Window(sf::VideoMode(Width, Height, 32), "SFML Sample Application");

    Window.setVerticalSyncEnabled(true);

    sf::Clock Clock;
	sf::Texture Texture1;
	if (!Texture1.loadFromFile("ball.png")) return 1;

	sf::Texture Texture2;
	if (!Texture2.loadFromFile("paddle.png")) return 1;

    sf::Sprite Sprite1;
    Sprite1.setTexture(Texture1);
    Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
    //Sprite1.setColor(sf::Color(0, 0, 0, 255));

    sf::Sprite Sprite2;
    Sprite2.setTexture(Texture2);
    Sprite2.setPosition(Texture2.getWidth(), Height / 2 - Texture2.getHeight() / 2);
    Sprite2.setColor(sf::Color(240, 0, 0, 255));

    sf::Sprite Sprite3;
    Sprite3.setTexture(Texture2);
    Sprite3.setPosition(Width - Texture2.getWidth() * 2, (Height / 2) - Texture2.getHeight() / 2);
    Sprite3.setColor(sf::Color(0, 0, 240, 255));

    sf::IntRect Sprite1Rect(Sprite1.getPosition().x, Sprite1.getPosition().y, Texture1.getWidth(), Texture1.getHeight());
    sf::IntRect Sprite2Rect(Sprite2.getPosition().x, Sprite2.getPosition().y, Texture2.getWidth(), Texture2.getHeight());
    sf::IntRect Sprite3Rect(Sprite3.getPosition().x, Sprite3.getPosition().y, Texture2.getWidth(), Texture2.getHeight());

    double speed = 5.f;
    bool IsMoving1 = false;
    bool IsMovingUp1 = true;
    bool IsMoving2 = false;
    bool IsMovingUp2 = true;
    bool HitLeft = true;
    int Player1wins = 0;
    int Player2wins = 0;

	while (Window.isOpen())
	{
	    double ElapsedTime = Clock.getElapsedTime().asSeconds() * 10;
	    Clock.restart();

		sf::Event Event;
		while (Window.pollEvent(Event))
		{
			switch (Event.type)
			{
			case sf::Event::Closed:
				Window.close();
				break;
            case sf::Event::MouseButtonPressed:
                Sprite1.setPosition(sf::Mouse::getPosition(Window).x - Texture1.getWidth() / 2, sf::Mouse::getPosition(Window).y  - Texture1.getHeight() / 2);
                break;
            case sf::Event::KeyPressed:
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                    {IsMovingUp1 = true; IsMoving1 = true;}
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                    {IsMovingUp1 = false; IsMoving1 = true;}

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                    {IsMovingUp2 = true; IsMoving2 = true;}
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                    {IsMovingUp2 = false; IsMoving2 = true;}
                break;
            case sf::Event::KeyReleased:
                IsMoving1 = false;
                IsMoving2 = false;
                break;
			default:
				break;
			}
		}

        std::cout << ElapsedTime << std::endl;

        Sprite1Rect.left = Sprite1.getPosition().x;
        Sprite1Rect.top = Sprite1.getPosition().y;

        Sprite2Rect.left = Sprite2.getPosition().x;
        Sprite2Rect.top = Sprite2.getPosition().y;

        Sprite3Rect.left = Sprite3.getPosition().x;
        Sprite3Rect.top = Sprite3.getPosition().y;

        std::cout << "Sprite3Rect: " << Sprite3Rect.left << ":" << Sprite3Rect.top << std::endl;
        std::cout << "Sprite3: " << Sprite3.getPosition().x << ":" << Sprite3.getPosition().y << std::endl;

        std::cout << "Sprite2Rect: " << Sprite2Rect.left << ":" << Sprite2Rect.top << std::endl;
        std::cout << "Sprite2: " << Sprite2.getPosition().x << ":" << Sprite2.getPosition().y << std::endl;

        std::cout << "Sprite1Rect: " << Sprite1Rect.left << ":" << Sprite1Rect.top << std::endl;
        std::cout << "Sprite1: " << Sprite1.getPosition().x << ":" << Sprite1.getPosition().y << std::endl;

		if (Sprite1Rect.intersects(Sprite2Rect))
		{
		    if (Sprite1Rect.left - 1.f <= Sprite2Rect.left + Texture2.getWidth() / 2)
		    {
		        std::cout << "Ball Hit Top/Bottom of Left Paddle" << std::endl;
                IsMoving1 = false;
		    }
		    else
		    {
                HitLeft = true;
                std::cout << "Ball Hit Left Paddle" << std::endl;
		    }
		}
		else if (Sprite1Rect.intersects(Sprite3Rect))
		{
            if (Sprite1Rect.left + Texture1.getWidth() / 2 >= Sprite3Rect.left - Texture2.getWidth() / 4)
		    {
		        std::cout << "Ball Hit Top/Bottom of Right Paddle" << std::endl;
                IsMoving2 = false;
		    }
		    else
		    {
                HitLeft = false;
                std::cout << "Ball Hit Right Paddle" << std::endl;
		    }
		}

		if (HitLeft == true)
		{
		    Sprite1.move(speed * ElapsedTime, 0);
		}
		else
		{
		    Sprite1.move(-1 * speed * ElapsedTime, 0);
		}

		if (IsMoving1 == true && IsMovingUp1 == true && Sprite2.getPosition().y >= 0)
		{
		    Sprite2.move(0, -1 * speed * ElapsedTime);
		}
		else if (IsMoving1 == true && Sprite2.getPosition().y <= Height - Texture2.getHeight())
		{
		    Sprite2.move(0, speed * ElapsedTime);
		}

		if (IsMoving2 == true && IsMovingUp2 == true && Sprite3.getPosition().y >= 0)
		{
		    Sprite3.move(0, -1 * speed * ElapsedTime);
		}
		else if (IsMoving2 == true && Sprite3.getPosition().y <= Height - Texture2.getHeight())
		{
		    Sprite3.move(0, speed * ElapsedTime);
		}

        if (Sprite1.getPosition().x <= (0.f - Texture1.getWidth()))
        {
            Player1wins++;
            Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
            HitLeft = true;
        }
        else if (Sprite1.getPosition().x >= Width)
        {
            Player2wins++;
            Sprite1.setPosition(Width / 2 - Texture1.getWidth() / 2, Height / 2 - Texture1.getHeight() / 2);
            HitLeft = false;
        }

        Window.clear(sf::Color(245, 245, 245));
        Window.draw(Sprite1);
        Window.draw(Sprite2);
        Window.draw(Sprite3);
		Window.display();

	}

	return 0;
}
Last edited on
cool will read it soon =D
and its 3 days a week for 8 hours each day...and the reason its so expensive is that the uni is a private course and they guarantee a job after course completion...due to contracts with game dev companies which they have close ties to but because they guarantee it there are not many students here cause there is only so many places..but yea thats my main reason for doing it
ah, thats cool, unless you end up being the guy who gets the coffee.

3x8 hours? nice, if my college dedicates the same amount of time to it that your university does maybe I will learn something after the first year.
you always end up being the guy who gets the coffee its like the rookie ritual lol
Yeah :/ except I'm lazy, so I would rather spend the next 3 or 4 years programming in c++ and try to make my own games, than work at some company as the coffee boy :b then again, might not have a choice, and there are worse things than getting coffee I suppose..
haha i'm a bit lazy to...man been up for way to long must finish this dam pong or i'm gunna fail sigh
Topic archived. No new replies allowed.