I am coding a game in C. Console or not? [closed] - c

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 9 years ago.
Improve this question
I was making some games using Game Maker, but I would like to make simple games using C. I am a newb C programmer. When I code the output is always in the CMD Windows Console. I could make some simple games but always in the console, which is not very flexible (you can't animate without scrolling). When you run a more complex game, a complete new screen is created and I actually interact whit that screen.
So the question that i am having using C language is: How is this new screen being loaded? I think there is some windows API to create a new screen or something. By the way, in the old times, I mean DOS time, you just used a console but you could load a new screen where the game was played. How can you achieve this?
I would like some guideline to research the root. I just don't want to call library X or use SDL.
Thanks your your help.

Since you mention library X i assume you are on a Linux system. There are several different ways to archive your goal. If you would like to still use the console but with more graphics involved you could take a look at library ncurses ( http://www.gnu.org/software/ncurses/ ).
If you want to do more advanced graphics i recommend you to take a look here : How do you create a window in Linux with C++?

C programming dates back to 1970's, so you can think of creating games similar to that era.
The window or the new screen is loaded by few preprocessor directives which are already compiled and stored by the developers.
A new screen can be achieved by giving a few commands:
#include<stdio.h>
#include<conio.h>
void main()
{
local declarations;
And your program for the game;
getch();
}
What this basically do is the function getch(); which is stored in (conio.h) will open a new window and it will display
the output till it gets a value from the keyboard ie it displays the output till you press any key from the keyboard.
hope this answer helps you.

Creating new Windows can get pretty hairy (ie. the code can be complicated and difficult to read). I'll try to give a somewhat high-level overview, then link you to a good tutorial for loading a basic window.
You need to declare a couple of variables of types HWND and WNDCLASSEX, and call a bunch of Windows API functions to initialize the Window with some settings and whatnot.
Once this is done, you need to enter a loop that handles all the window interactions, usually with TranslateMessage and DispatchMessage inside the loop.
You also need to implement a callback procedure for handling Windows events such as mouse clicks, closing the window, etc. This is usually done in a LRESULT CALLBACK WndProcedure.
I've now thrown a bunch of new words and ideas around with little explanation; check out this tutorial for a full example of what I've just tried to explain.
http://www.functionx.com/win32/Lesson01c.htm
(EDIT: the link above is dead now, so here's a link to a cache of it from the Wayback Machine https://web.archive.org/web/20190203182520/http://www.functionx.com/win32/Lesson01c.htm)
Hope this helps!
Also - this is all assuming you're on Windows based on your comment about a Windows API (ie. windows.h). If you're on Linux, you'll need to use X.

Related

What is the most efficient way to take in user input with many options in C? [closed]

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 2 years ago.
Improve this question
I am writing a weather program that calls an API for data. One of the flags available is a preferred language, of which there are about 45 options. This leads me to the question.
What is the most efficient way to display all the language options, then allow user input, then check for valid input?
My best idea is a loop that prints all the options from a file. The user then inputs an option. Their selection is checked against the list to find a match. If there is a match then the program continues. If not, they are prompted again.
Is this the best way to go about this? I'm trying to make this program as efficient and professional looking as possible as I'm using it for my portfolio.
My best idea is a loop that prints all the options from a file. The user then inputs an option. Their selection is checked against the list to find a match. If there is a match then the program continues. If not, they are prompted again.
Is this the best way to go about this? I'm trying to make this program as efficient and professional looking as possible as I'm using it for my portfolio.
There are always multiple competing goals (single-thread performance, scalability, features, flexibility/extendibility, code readability, fault tolerance). For well designed code, its good to understand the importance of each of these goals for each piece of code (and good to understand that these importances can be different for different pieces of code in the same project). For this specific piece of code; I'd say that flexibility/extendibility (e.g. the ability to add new languages easily later) is the most important, followed by code readability (the ability to understand the code later, and find/fix bugs in it). The least important are scalability (e.g. how much performance increases when number of CPUs increases) then single-thread performance; because the code only needs to work once and is held back by the speed that a human can type anyway.
Is this the best way to go about this? I'm trying to make this program as efficient and professional looking as possible as I'm using it for my portfolio.
In terms of "human computer interaction"; the best way is to make it impossible for the user to enter invalid data (e.g. a drop down list with a well predicted default to avoid the need for a "not set yet" option). The second best way is "active status" - specifically, for every "user input event" (key press, mouse click, etc) a status field corresponding to the input field/control is updated to either indicate that the field/input is in an acceptable state, or provide the reason why it's not; where its impossible for the user to continue (e.g. because an "OK" button is disabled) until all of status fields are saying that the input is acceptable. For both of these options there is no need to validate the submitted input afterwards.
Sadly; for "command line", it's almost impossible to use the best way and almost impossible to use the 2nd best way.
In other words; you need to forget about performance/efficiency (because that's the least important); and then forget about writing software that is good/user-friendly (because it's command line).
The question then is; what is the "least bad" option? For this; I'd start by assuming that the data for each language is stored in a separate file (or directory?) where the file name is usable for display purposes; and all of the data is in a specific directory (e.g. a "project/lang" directory that contains a "project/lang/UK_English" file, a "project/lang/Spanish" file, etc). In this case you can get a list of files in the "project/lang" directory, sort them in alphabetical order, and use them to display a list of numbered options ("1) Spanish", "2) UK English", ..). Then if/when the user selects an option you can validate it (and report any errors if the user entered a bad character, a number that's too high, etc, then ask the user to retry); and load the right file for whichever language they chose (and report any errors if there's a problem with the file and ask the user to choose something else).
That way; people/translators can just create new files, and none of the code will need to be modified.
For a comparison; the fastest way is to use constant strings (e.g. puts("1) Spanish\n2) UK English\n\nEnter language choice:")); and to predict what the user will choose (e.g. based on keeping track of what they chose last time) and "pre-fetch and pre-parse" in the background (so that hopefully all the work is done for the correct choice before the user actually makes a choice), with the ability to quickly cancel the "pre-fetch and pre-parse" work if the user makes a choice that wasn't predicted. This would be extremely good for performance (likely "instant") but extremely bad (inflexible, over-complicated, too hard to maintain).

any tips to make my CMD game look better? [closed]

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 2 years ago.
Improve this question
I'm making a RPG game in batch, and the game looks bad, like the text layouts, does anyone have any design tips so my game looks more aesthetically pleasing?
I gave my students task to create I/O interactive, Terminal Based game, and some of students actually came back with fun results.
Use ASCII art. you can add different characters or scenes using ascii arts. you can find different arts on webpages like here and here. also you can convert your images to ASCII arts, that way you will be able to display any some images on Command Prompt
use ASCII Frames/borders for text and questions displays. you might need to resize frames by adding more characters, so it will fit your text.
use different colors to display different options, or underline good/bad events. I'm not sure about windows CMD but i tried on Unix and it works and looks pretty fun
Use Animations. Animate some lines by deleting line and redrawing/rewriting them. this way you will be able to receive Animation like results. its pretty easy, but you will need to store amount of characters outputted in line, so you will be able to clean exact amount of characters, otherwise you can clean full line
Here is example game that is fully created using ASCII art and it actually is pretty fun.
small examples from one of students i could find:
hope you'll find what you are looking for!

Detect Key Press on Windows Without Window Focus [closed]

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 6 years ago.
Improve this question
I've been searching online but all the top results on Google only lead me to Java. This is frustrating.
What I want to do is: make a program that listens to keyboard events, without being the active program. It has to work on at least windows 7, using C.
For example lets say I have myprogram.exe and other.exe. I want to be able to run both of them simultaneously, and have focus on other.exe, then press keys, and have myprogram.exe which runs on the side display which keys I pressed and log them.
If somebody has a link to a guide or information that explains what I should use to make this, that would be grand. If you can write up an explanation yourself that would be even better, but I don't mind going through documentations as long as they're relevant.
I've written games in C that listen to input from the active window, but I'm not sure how to poll events when the window isn't focused.
If you want to detect 'key press' events occurred in other processes, you should implement Global Hook. You can define a callback function for keyboard input events using SetWindowsHookEx().
Note that the callback function must be in a DLL in order to make it Global Hook.
So your myprogram.exe should link a dll implementing the hook. Then myprogram.exe would be able to detect any keyboard events on Windows.
Following is a good example with an explanation.
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks

Fastest way to capture what's on the screen in C

My goal is to write a program that would capture the screen in an efficient way.
Little twist, the screen will not be saved. My program will do various check on it (pixel's colors in some area.. etc).
The program will run in windows, and will need to take(and analyze) as many screenshot as possible per second, and will not be used in games. (That imply that I need the whole screen, like pressing prntscreen. It's not a problem if that fail in fullscreen game.)
All propositions are welcome, and I'd be glad to share any missing details.
Edit
As I wrote in the comment, capturing the screen for storing purpose is really common and easy to find.
I asked to be sure to not miss any method of capturing the screen without storing it at all.
The program will look the screen, take some decision then will quickly go to the next frame.
Perhaps look into SDL
A guick google returned this screenshot code for SDL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/2000-August/011387.html

What to program for a gadget that can deduce what I'm doing? The ultimate life-hack? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
This isn't really a programming question, more of an ideas question. Bear with me.
My sister gave me a well-used Nokia N95. I don't really need it, but I wanted it to do some programming for it. It supports a few languages, of which I can do Python.
My question is this: what to do with it? If I think about it, it has a lot to offer: i can program the GPS, motion sensor, wireless internet, sound and visual capture; it has a lot of hard disk space, it plays sound and video and so on.
The combinations seem limitless. The way I see it, it is a device that is easily always on me, has access to a huge data repository (the internet, and my personal data in it) and can be aware if I'm sitting at home, at work, or moving about somewhere. It could basically read my google calendar to check if I should be somewhere I'm not -- perhaps give me the bus schedule to get to where I should be. It could check if it's close to my home and therefore my home PC bluetooth/wifi. Maybe grab my recent work documents from my desktop computer, along with the latest Daily Show, for the bus journey to work. It could check my library account to see if any of my books are due, and remind me to take them with me in the morning. Set up an alarm clock based on what shift I have marked in my google calendar.
Basically I have a device that can analyze my movements in time (calendars with my data etc) and space (gps, carrier cell ids). By proxy, it could identify context situations -- I can store my local grocery store gps coordinates or cell mast ids and it could remind me to bring coffee.
Like I said, the possibilities seem limitless, and therefore baffling. Does anyone else have these pseudofantastical yearnings to program something like this? Or any similar ideas? How could this kind of device integrate into -- and help -- your life?
I'm hoping we could do some brainstorming.
"Gotta Leave" - A reminder that figures out the bus time, how far you are from a stop on your bus and shows a countdown till you "Could" leave (green), "Should" Leave (yellow), "Must" leave (orange), and "Gotta Run to get there" (red).
As inputs it needs what bus number you want to ride. You turn it on, it finds you, finds your closest few bus stops, estimates your walking speed at 2/mph and calculates when you need to leave where you are to get to the bus with 5 minutes waiting or less.
You should just pick any one and implement it.
It doesn't matter where you start, more that you actually do start. Don't concentrate on the destination, take a step and see what the journey holds.
Do it for a laugh to start and your expectation will be set right for both when you do find your killer app and when you don't.
"Phone home" - an interface to report home if you send a message to your phone that it is lost / stolen. Must be a silent operation from the phone holder's perspective
Options:
Self destruct mode to save your data from prying eyes
Keep calling with it's location every 10 minutes until an unlock is sent indicating the phone is found.
This is the same problem I face with the android (albeit java instead of python). The potential is paralyzing :)
I'd recommend checking out what libraries have already been written for doing cool stuff on that phone, and then building off of them- It's a system that provides inspiration, direction, and a good head start. For instance, on the android side, I'm fooling around with "zxing", a library that lets you read barcodes via the cellphone's camera. That's it's own sub-universe of possibilities, but at least it gives me a direction to go. "do cool things with information about products physically nearby"
"Late for Work" - Determines if you are not at work, buzzes you with a reminder and preps the phone to call into the sick line. Could be used if you are going to be late as well.
Inputs: Your sick line number. Time you should be at work. Where your home is, where your work is
Optional:
Send a text message
Post to an online in/out board
If you are still at home, sound an alarm
If you are still at home, call in sick, if you are not at home sent a "I'm going to be late" message
Comedy Option:
- If you don't respond to ten alarms, dial 911
To add on to what others have said, come up with some kind of office-GPS (via WiFi maybe? Does it have WiFi?) and tell you when you need to go to a meeting.

Resources