Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm making a simple game using CSFML and am wondering if I can make my code more efficient.
Right now the code looks like this inside the main function's game loop:
// game logic
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
}
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
I was told to do things in the order: logic, clear, draw, display. However, would there be any issues with me putting the logic inside the draw loop like this?
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
Would it slow down the draw calls or make the code more efficient?
I'm not sure if this specific library may be different from others, so even a general answer is appreciated.
How to structure your game is your design decision.
You will be the first to notice problems with it.
But here is my experience. I learned that in the game logic loop you have calculations which are definitly needed, they also tend to need information on how much time has passed. In the drawing loop you just need to visualise the result of the calculations. As soon as you are going to split into threads, the timing of the need to calculate and to visualise will change. You are likely to want to calculate more often than you draw, while you want to draw always completly (except for more advanced FPS-based level of detail rendering....).
So there are multiple aspects which might make you want to keep those two things apart.
What I did was having a calculation step (which by my design gets real time information as a parameter of elapsed seconds) and a separate drawing step. That way you can have them in one parent loop, but you are free to move them at any point AND you are protecting yourself from confusing those two steps.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
So I am making a snake game in C but the problem is that it causes a lot of flickering and it also slow downs the game, So I heard that double buffering can solve this but I don't know how to implement that and other ways will also be appreciated. For now I have just reposition the cursor to 0 and disable the visibility of cursor.
The way games typically prevent this "flickering" is due to double buffering. As the current view is being rendered, the back view is performing updates. When the two views swap, the back view clears and performs the updates for the next frame while the current view is being rendered from the previous frame. In Console, you don't have this luxury and when I was writing my snake game in terminal I solved it by not redrawing the entire console every "frame". Rather, I simply drew an empty space over the last tail position and kept the rest of the frame the same. Similar with the snake going over the food, just draw over it with an empty space.
I believe the function I had written was something like this.
void clearlastposition(GameState* state){
_COORD last;
last.X = state->snake.back().partposition.x;
last.Y = state->snake.back().partposition.y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),last);
printf(" ");
}
In my design, the snake body was a vector of snake parts. Where each part contained an x and y position. And the GameState just held an instance to the snake body.
This was done on Windows.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to create a timer to display in the top right hand corner of a game that counts from 99% down to 0% with 1% being dropped every second and the counter refreshing on the screen. ps (i'm new to coding so sorry if this is a dumb question)
I've tried using a simple countdown loop but it decreases all at once, and doesn't save the counter as a variable, which i need so i can print it at a specific point on the screen
void battery_life(){
int w, h;
get_screen_size(&w, &h);
int y=99;
while (y !=0)
y--;
draw_int (2*w/3+5,h-h+2,y);
draw_char(2*w/3+7,h-h+2,'%');
}
I expect that when i run this counter it -1% every second but it just counts down all at once and displays 0%
It's a rather broad question, but here are several general approaches, which all need to:
Separate the game logic from rendering, and
Using a state machine for updating game state, instead of pausing the execution.
Here are two different approaches:
Game loop
Since you are designing a game, it's likely you will have a game loop of some sort, i.e.:
while (!ending)
{
process_input();
update_state();
draw();
}
And your update_state and draw functions should have different responsibilities, similar to:
static int current_percentage = 100;
void update_state(void)
{
// keep the time of last update
static timestamp last_update_time = 0;
// decrease 'current_percentage' when needed
timestamp now = get_current_game_time();
if (seconds_elapsed(now, last_update_time) >= 1)
{
last_update_time = now;
current_percentage--;
}
}
void draw(void)
{
// just handle the drawing
draw_percentage(current_percentage);
}
Multiple threads
On the other hand, if you are using multiple threads, you would likely end up with a rather different approach:
static atomic_int current_percentage = 100;
void decrease_percentage(void)
{
atomic_fetch_sub(¤t_percentage, 1);
}
void draw(void)
{
// just handle the drawing
int percentage = atomic_load(¤t_percentage);
draw_percentage(percentage);
}
// no game loop - we will need to configure function callbacks
// for different events
void on_game_start(void)
{
// one thread should update the timer
call_periodically(1000 /* period in ms */, decrease_percentage /* what to call */);
// this should run at max possible fps
call_when_idle(draw /* what to call when idle */);
}
The latter approach is slightly shorter, although the main reason for this is because a lot of functionality is hidden inside the functions which configure the callbacks. The former one is simpler to reason about and doesn't have issues which arise with multithreaded code (deadlocks, race conditions - note the use of the atomic_int to ensure atomic updates), which is why a game loop is preferred if possible.
If your game needs to utilize multiple cores, then you will need to involve some sort of threading, which comes with its additional complexity. The implementation of the second approach would also depend on your platform, since timers are not a part of the C standard.
Finally, if you are using a game engine/framework (libgdx, Unity), you will likely have to implement the update and draw callbacks only, with the framework taking care of calling these functions when needed.
A couple things:
Your while does nothing. If you don't put your code inside brackets, it only executes the next line in a loop.
You're not waiting for a second anywhere.
You should probably rethink your approach - if you fix the above problems, when you call the function everything else in your program will freeze for 99 seconds except for the battery text changing.
Maybe a better approach would be to write a function that has a static int y = 99, and each time you call it draw the battery text and decrease y by 1 (static more or less means the value is remembered between calls).
Then, in your main game loop, you can check if a second has passed, and if so, execute that function.
You are missing brackets from your while loop so it's not going to work for a start!
You really need to implement a timer so that you can actually tell when a second has passed as your code could vary in operation speed, and definitely will when you make any changes.
How you do this is totally up to the system you are writing on and you haven't given enough information for us to help you.
Most often hardware running C has hardware timers and specific registers you need to target.
If you are using unity, You can simply use a coroutine to countdown percentage per seconds.
void countDown()
{
int percent = 99;
StartCoroutine(CountDownToZero(percent));
}
IEnumerator CountDownToZero(int percentage)
{
WaitForSeconds wait1sec = new WaitForSeconds(1f);
while(percentage > 0)
{
percentText.text = percentage.ToString()+"%";
yield return wait1sec;
percentage= percentage- 1;
}
percentText.text = "0%";
}
In case of other engines, they also provide scheduler to schedule the execution of functions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have been given the assignment to create a quite complicated to me, but simple to some C game. The program will run and generate 25 random numbers between 1 to 100 (no repeats). The game is between two users.
Both the players will get two guesses each time. Every correct guess will be counted as 1 correct answer and will be displayed on the screen in the board.
The player having more number of correct guesses will be a winner.
The game will continue until the board is completely revealed.
The first screen should ask the user for his name, print a welcome message, and displays an empty 5x5 board. But this 5x5 board has the values in it internally
Now the program should ask the number of players (1 or 2)
If one, ask one name and second is computer and if 2 ask two names
For playing against computer, you are asked for two guesses and every correct guess is shown on the board.
Now 2 guesses for computer would be taken and shown on the screen.
I have tried everything to my knowledge to complete this but I lack a full understanding of C. Any help would be appreciated. I did not include my code because, honestly its just a mess and does not even run.
I would like to see someone be able to make such a game, so that I can study the logic. NOT COPY THE WORK
Simple steps. Start small and grow.
Easy one is to remove all questions to the user(s). Hard code the answers in the program. You can retrofit the IO later.
Start with a way ot generating 25 random numbers and load them into an array. Place the array in a global variable. You need a another array to show when a number is sucessfully guessed.
Now write a function to display that guessed array as 5X5.
Gradually build the program up
Always make functions
generate_array
show_guessed
....
If get stuck on specific things then post a new question.
This is not a direct answer, but a very long comment with some requests for more information and effort.
The problem I have with your request is that I see zero effort. I see a request for teh C0d3z and a promise not to cheat after receiving something that makes it far too easy to cheat.
What I, and probably others here, want to see is effort and some attempt.
Do you know how to print text to the screen like you see in the requirements? Do you know how to print text at all? If so, make the print routines and state this.
Do you know how to generate random numbers? If so, say so in your question and we see effort.
Do you know how to receive input from the user?
Do you know what an array is?
Do you know how to save code in your editor and compile it?
You can see that without any sort of background or starting code, we don't know where you are in your learning.
So...
Post an attempt at solving your problem in your question. If you are truly so new to coding that you cannot do this, then you need to sit down with your professor / TA / whomever and tell them this. Reading through your teaching material (textbook or whatever) should help a whole lot as well. This assignment feels like something I would see towards the end of a beginner's C class. If this is where you are, and you really have no clue what to do, then you may need to retake the class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!
If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P
What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 13 years ago.
Improve this question
Although it's true that some
recursive-nameserver configurations
are (sloppily) referred to as
"caching", e.g., by
RHEL/Fedora/CentOS, that's a really
bad name for that function -- because
caching is orthogonal to recursion.
Theoretically, you could write a
nameserver that does recursive service
but doesn't cache its results. (That
would be a bit perverse, and I don't
know of any.) Conversely, nameserver
packages that cache but know nothing
about how to recurse and instead do
less-helpful alternative iterative
service are common: dnsmasq, pdnsd,
etc. ... ...
Above text source: http://linuxgazette.net/170/lan.html
Please explain what does the author means by "caching is orthogonal to recursion" ?
From Wikipedia's definition of orthogonal:
For example, a car has orthogonal
components and controls (e.g.
accelerating the vehicle does not
influence anything else but the
components involved exclusively with
the acceleration function).
The author is saying that whether a nameserver caches is nothing to do with whether it can recurse.
caching is orthogonal to recursion?
Caching doesn't require/imply recursion.
The term "orthogonal" is meant to be interpreted from a mathematical sense loosely has "the things have nothing in common i.e. separate concepts".
It mean it is one feature is independent with the other one. Or have bothe feature have no influence with the other one. So they can be implemented independantly
in a programming point of view, two orthogonal features
do_work(bool feature1, bool feature2)
{
// do common work
if(feature1)
{ //... do this }
// do common work
if(feature2)
{ // do work }
// do common work
}
or: if they are not orthogonal:
you need to do this: ( and it may have case where you cant combine the two feature.
do_work(bool feature1, bool feature2)
{
if(not feature1 and feature 2)
{ //... do this }
else if(feature1 and not feature2)
{ // do work }
// else impossible or different behavior
// etc..
}