Skip to main content
gamedev.net
🔒 Locked

C++ : help required with loop

Started by penetrator 3 replies 500+ views
Original Post
penetrator
penetrator
Hi, i need to load about 200 files as follows LoadTGA(&explosion[0], "textures/000.tga"); LoadTGA(&explosion[1], "textures/001.tga"); .................................. LoadTGA(&explosion[199], "199.tga"); Instead of writing 200 lines, i'd like to build a simple loop, but i can't figure out how to "feed" the name file to the instruction. I need something as: for (int i=0; i<200; i++) { LoadTGA(&explosion, filename); } how can i define the filename ?
doynax
doynax
Something like this should work.
#include <stdio.h>void LoadExplosion() { int i; char name[sizeof("textures/???.tga")]; for(int i = 0; i < 200; ++i) {  sprintf(name, "textures/%03d.tga", i);  LoadTGA(&explosion, name); }}
snk_kid
snk_kid
If your using c++ then can do it like this:
#include <cstdlib>#include <sstream>#include <string>#include <vector>#include <iostream>int main() {  std::vector<std::string> v(200);  for(int i = 0, n = v.size(); i < n; ++i) {      std::ostringstream oss;      oss << i << ".tga";      v = oss.str();  }  for(int i = 0, n = v.size(); i < n; ++i) {     std::cout << v << '\n';  }  return EXIT_SUCCESS;}


And if you are using c++ prefer using references instead of pointers in your LoadTGA function if your only passing one instance.
DigitalDelusion
DigitalDelusion
this should do the trick:
void LoadExplosion(void){	char sz[] = "textures/000.tga";	char* p = sz + sizeof(sz) - 8;	for(int i = 0; i != 200; ++i)	{		sprintf( p, "%03d.tga", i);		LoadTGA(explosion + i, sz);	}}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
penetrator
penetrator
Thank you guys !

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.