Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Draw Missing!?  (Read 34834 times)

0 Members and 4 Guests are viewing this topic.

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Draw Missing!?
« Reply #60 on: August 22, 2008, 12:39:11 am »
Quote from: "qsik"
how descrptive should the comments be? could u give an example


Don't comment every line to the point of obviousness.  i.e. don't do this:

y = x + z;//add x and z and store result in y

Do separate your code into "paragraphs"  that performs a single task (potentially many of these in a function) i.e.

//loads needed images from HD
for (unsigned int i = 0; i < vImageFileNames.size(); ++i)
{
    if (!LoadImageFromFile(vImageFileNames))
        return false;
}


This is useful to provide "tabs" for code that is easy to understand how it works if you read it, but it makes finding the code you want much easier.

If you have code that is not necessarily easy to understand commenting every line or better may be necessary (as well as good variable names).
For example, if I was writing say a collision detection routine with some complicated maths, I'd probably comment every line to explain the process.  Sometimes I've even done ASCII art in my comments to visually explain some computational geometry.

Also, if there ever a specific reason you did something that would not be readily apparent, comment it.  For example if you call two functions and the second is dependent on the first (but not in an obvious way).  examples:

//the behavior of bar() depends on foo(), so call it first
foo();
bar();


//we skip the first to elements because...
for (unsigned int i = 2; i < m_vMyVector.size(); ++i)
{
    //some code...
}


In general "paragraph tabs" (my personal word for them) should always be used and make finding the relevant code easier and line-by-line comments are for code that's hard to follow.

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Draw Missing!?
« Reply #61 on: August 23, 2008, 07:13:10 am »
how's this for a first comment attempt?

Code: [Select]
#include "SFML/Quickstart.h"
#include "SFML/String Conversion.h"

int CheckCategories[6];
int CheckMoney[30];

std::string Path;

sf::Sprite Other[6];
sf::Sprite Categories[6];
sf::Sprite Money[30];
sf::Sprite Question;
sf::Sprite Answer;

sf::Image other[6];
sf::Image categories[6];
sf::Image money[5];
sf::Image question;
sf::Image answer;

sf::Music Music[4];

//Function returning the current screen resolution's width
int GetDesktopWidth()
{
sf::VideoMode DesktopMode = sf::VideoMode::GetDesktopMode();
return DesktopMode.Width;
}

//Function returning the current screen resolution's height
int GetDesktopHeight()
{
sf::VideoMode DesktopMode = sf::VideoMode::GetDesktopMode();
return DesktopMode.Height;
}

std::string GetExeD()
{
GetModuleFileName(NULL, L"Path", 100);
return Path;
}

//Load images from subfolders of /Jeopardy
int LoadImages()
{
std::string base = "C:/Jeopardy/";
std::string end = ".bmp";
std::string middle;
std::string image;

for(int i = 0; i < 6; i++)
{
middle = Convert((i + 1));
image = base + "Other/Slide" + middle + end;
if (!other[i].LoadFromFile(image))
return -1;
image = base + "Categories/Slide" + middle + end;
if (!categories[i].LoadFromFile(image))
return -1;
};

for(int i = 0; i < 5; i++)
{
middle = Convert((i + 1));
image = base + "Money/Slide" + middle + end;
if (!money[i].LoadFromFile(image))
return -1;
};

return 1;
}

//Assign all loaded images to sprites
void LoadSprites()
{
for (int i = 0; i < 6; i++)
{
Other[i].SetImage(other[i]);
Categories[i].SetImage(categories[i]);
};

for (int i = 0; i < 5; i++)
{
Money[i].SetImage(money[i]);
Money[(i + 5)].SetImage(money[i]);
Money[(i + 10)].SetImage(money[i]);
Money[(i + 15)].SetImage(money[i]);
Money[(i + 20)].SetImage(money[i]);
Money[(i + 25)].SetImage(money[i]);
};
}

//Scale sprites to have each fit 1/6 of the screen resolution
void ScaleSprites()
{
int ScreenWidth = GetDesktopWidth();
int ScreenHeight = GetDesktopHeight();
float ImageWidth;
float ImageHeight;

for (int i = 0; i < 6; i++)
{
ImageWidth = other[i].GetWidth();
ImageHeight = other[i].GetHeight();
ImageWidth = (ScreenWidth / ImageWidth);
ImageHeight = (ScreenHeight / ImageHeight);
Other[i].Scale(ImageWidth, ImageHeight);

ImageWidth = categories[i].GetWidth();
ImageHeight = categories[i].GetHeight();
ImageWidth = ((ScreenWidth/6) / ImageWidth);
ImageHeight = ((ScreenHeight/6) / ImageHeight);
Categories[i].Scale(ImageWidth, ImageHeight);
};

for (int i = 0; i < 30; i++)
{
ImageWidth = money[0].GetWidth();
ImageHeight = money[0].GetHeight();
ImageWidth = ((ScreenWidth/6) / ImageWidth);
ImageHeight = ((ScreenHeight/6) / ImageHeight);
Money[i].Scale(ImageWidth, ImageHeight);
};
}

//Position sprites evenly across the screen in a 6 x 6 grid
void PositionSprites()
{
int ScreenWidth = GetDesktopWidth();
int ScreenHeight = GetDesktopHeight();
float xpos = (ScreenWidth/6);
float ypos = (ScreenHeight/6);
float x = 0;
float y = ypos;

for(int i = 0; i < 6; i++)
{
Categories[i].SetPosition(x, 0.0f);
x = x + xpos;
};

for(int i = 0; i < 5; i++)
{
Money[i].SetPosition(0, y);
Money[(i + 5)].SetPosition(xpos, y);
Money[(i + 10)].SetPosition((xpos * 2), y);
Money[(i + 15)].SetPosition((xpos * 3), y);
Money[(i + 20)].SetPosition((xpos * 4), y);
Money[(i + 25)].SetPosition((xpos * 5), y);
y = y + ypos;
};
}

//Load music
int LoadMusic()
{
if (!Music[0].OpenFromFile("C:/Jeopardy/Music/Opening.wav"))
return -1;

if (!Music[1].OpenFromFile("C:/Jeopardy/Music/Question.wav"))
return -1;

if (!Music[2].OpenFromFile("C:/Jeopardy/Music/Daily Double.wav"))
return -1;

if (!Music[3].OpenFromFile("C:/Jeopardy/Music/Final Jeopardy.wav"))
return -1;

return 1;
}

//Check to see if sprite has been clicked or not
void InitChecks()
{
for(int i = 0; i < 6; i++)
CheckCategories[i] = 1;
for(int i = 0; i < 30; i++)
CheckMoney[i] = 1;
}

//Execute all the functions above and play the Opening Music
int Load()
{
GetExeD();
LoadImages();
LoadSprites();
ScaleSprites();
PositionSprites();
LoadMusic();
InitChecks();
Music[0].Play();

return 1;
}

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Draw Missing!?
« Reply #62 on: August 25, 2008, 04:56:26 pm »
Quote from: "qsik"
how's this for a first comment attempt?


The function header comments are a good idea.   You might consider more intra-function comments.  Over each for-loop, for example (unless it's simple and obvious).
Any time you hard code a value, it's probably a good idea to explain.  What's the meaning of (i + 5), (i + 10), etc. in your LoadSprites function.  Everyone's commenting is different, but here's a sample from my code:

Code: [Select]

//draws a grid for the terrain tiles in the specified color.
void Renderer_Editor::DrawTerrainGrid(sf::Color gridColor)
{
//needed whether we are initing lines or not
int nTileSizeY = TerrainManager::Instance()->GetTileSizeY();
int nTileSizeX = TerrainManager::Instance()->GetTileSizeX();

//Init lines on first draw request.  (We can't do in init since it depends on TerrainManager, which is inited later.)
if (m_bInitGridLines)
{
sf::Vector2i v2iWinSize = this->GetOriginalWindowSize();

//some other data we need
int nHalfWinSizeX = v2iWinSize.x / 2;
int nHalfTileSizeY = TerrainManager::Instance()->GetHalfTileSizeY();

//we need to draw another half-screen further since the later lines are partially off-screen
//TODO: readjust this when resizing window
m_nMaxYOffset = v2iWinSize.y + nHalfWinSizeX + 1 + TerrainManager::Instance()->GetTileSizeY();

//init the grid-line members
//the southwest to northeast grid lines
m_SWtoNEgridLine = sf::Shape::Line(0.0f, (float)nHalfTileSizeY, (float)(v2iWinSize.x + (nTileSizeX * 2)), (float)(nHalfTileSizeY - nHalfWinSizeX - (nTileSizeY * 2)), 1, gridColor);
//the northwest to southeast grid lines
m_NWtoSEgridLine = sf::Shape::Line(0.0f, (float)(nHalfTileSizeY - nHalfWinSizeX), (float)(v2iWinSize.x + (nTileSizeX * 2)), (float)(nHalfTileSizeY + (nTileSizeY * 2)), 1, gridColor);

m_bInitGridLines = false;
}

//actually draw the grid lines
//each frame we offset by the scroll offset up to 1 tile, when we go back to 0 (So the same grid stays alligned and on-screen)
sf::Vector2f v2fTileOffset(fmod(m_v2fScrollOffset.x, (float)nTileSizeX), fmod(m_v2fScrollOffset.y, (float)nTileSizeY));
int nYOffset = -nTileSizeY;

//draw a gridline on both diagonals at every tile boundry across the screen height
while (nYOffset < m_nMaxYOffset)
{
m_SWtoNEgridLine.SetPosition(m_v2fScrollOffset.x - v2fTileOffset.x - nTileSizeX, (float)nYOffset + m_v2fScrollOffset.y - v2fTileOffset.y);
m_NWtoSEgridLine.SetPosition(m_v2fScrollOffset.x - v2fTileOffset.x - nTileSizeX, (float)nYOffset + m_v2fScrollOffset.y - v2fTileOffset.y);
m_pRenderWindow->Draw(m_SWtoNEgridLine);
m_pRenderWindow->Draw(m_NWtoSEgridLine);

nYOffset += nTileSizeY;
}
}

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Draw Missing!?
« Reply #63 on: August 29, 2008, 06:00:09 am »
i still cant figure out how to be able to use std::strings in GetModuleFileName() or convert TCHARS to std::strings

also, can someone explain the poor quality of the text in the images of my program (they are really pixelated on my 1024 x 768 screen)

**why does everything on the site seem so much smaller?

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Draw Missing!?
« Reply #64 on: September 01, 2008, 04:28:54 am »
a little help?

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Draw Missing!?
« Reply #65 on: September 01, 2008, 10:56:25 am »
Quote from: "qsik"
i still cant figure out how to be able to use std::strings in GetModuleFileName() or convert TCHARS to std::strings


These are WIN32 API function i guess? .. you should look them up on MSDN or something :) and i believe there might be another win32 function which can convert TCHAR to a char*
//Zzzarka

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Draw Missing!?
« Reply #66 on: September 01, 2008, 07:35:08 pm »
Have you tried using a std::wstring?

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Draw Missing!?
« Reply #67 on: September 04, 2008, 01:18:29 am »
can i use a wstring in sf::LoadImageFromFile()?

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Draw Missing!?
« Reply #68 on: September 04, 2008, 06:49:24 pm »
There is many ways I can think of on how to do that...


Have you even tried it?
Code: [Select]
sf::Image    Image;                // The image to load
std::wstring Filename(L"foo.png"); // The filename of the image

// Load the image using a std::wstring of the filename
Image.LoadFromFile(Filename);


You might be able to simply use std::wstring::c_str():
Code: [Select]
sf::Image    Image;                // The image to load
std::wstring Filename(L"foo.png"); // The filename of the image

// Load the image using a c-style string of the filename
Image.LoadFromFile(Filename.c_str());


If that still doesn't work, it will work by doing this:
Code: [Select]
sf::Image    Image;                 // The image to load
std::wstring WFilename(L"foo.png"); // The filename of the image

// Convert WFilename from std::wstring to a std::string called Filename
std::string Filename;
Filename.assign(WFilename.begin(), WFilename.end());

// Load the image using a std::string of the filename
Image.LoadFromFile(Filename);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Draw Missing!?
« Reply #69 on: September 04, 2008, 10:52:26 pm »
SFML takes simple strings (std::string, const char*), not wide ones (std::wstring, const wchar_t). So your 2 first solutions won't work. And the third one doesn't make much sense, in the contexts where this simple conversion works you can directly use simple strings.
Laurent Gomila - SFML developer

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Draw Missing!?
« Reply #70 on: September 05, 2008, 07:39:44 pm »
The problem is that he is getting TCHARs from the Windows API that are wchar_t*.
So, he must convert the wchar_t*s to char*s somehow in order to use them with sf::Image::LoadFromFile.
Perhaps he should just turn Unicode support off, but I think he may need it for some reason.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Draw Missing!?
« Reply #71 on: September 05, 2008, 07:47:48 pm »
Unicode is enabled by default on some versions of VC++, so in general people can just turn it off.

However, SFML provides functions to properly convert from wide strings to simple strings. Try that :
Code: [Select]
const wchar_t* w = L"some text";
std::string s = sf::Unicode::Text(w);

In fact, Text is a classe which can be constructed from and converted to any type / encoding of strings (including UTF standards). It's mainly used internally, but it can be used to easily perform this kind of conversions as well.
Laurent Gomila - SFML developer

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Draw Missing!?
« Reply #72 on: September 07, 2008, 12:10:36 am »
i know unicode allows for international characters but does turning it off do anything that would majorly affect my program?

otherwise...i could have fixed this a long time ago

 

anything