Skip to content

Commit

Permalink
SDL TTF text example
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Jul 3, 2016
1 parent f48340d commit e52ed4b
Show file tree
Hide file tree
Showing 32 changed files with 452 additions and 18 deletions.
8 changes: 7 additions & 1 deletion c/operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,13 @@ int main() {
}
}

/* # Bitwise operators */
/*
# Bitwise operators
On signed integers: implementation defined or UB:
http://stackoverflow.com/questions/11644362/bitwise-operation-on-signed-integer
so just never do it.
*/
{
/*
# ~
Expand Down
1 change: 1 addition & 0 deletions gcc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Cheat on the GNU Compile Collection (GCC) language extensions and command line u
1. [Compilation steps](compilation-steps.md)
1. [Invocation](invocation.md)
1. [Spec files](spec-files.md)
1. [GCC as library](gcc-as-library.md)
1. Extensions
1. Types
1. [Empty struct](empty_struct.c)
Expand Down
55 changes: 46 additions & 9 deletions gcc/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

#include "common.h"

int main() {
int main(void) {
#if defined(__i386__) || defined(__x86_64__)
puts("__i386__ || __x86_64__");

Expand Down Expand Up @@ -169,7 +169,7 @@ int main() {
gcc will automatically put the value of `in` from RAM into a register for us
and `out` from a register into ram at the end
note how we can do an `inc` operation directly on `%1` and `%0`
We can do an `inc` operation directly on both `%1` and `%0`,
so they must both already be inside a registers as expected
GCC just makes sure they are written from/to memory before/after the operations.
Expand All @@ -185,6 +185,9 @@ int main() {
: "=r" (out)
: "r" (in)
);
/* in was not modified by inc. It had already been moved to a register. */
assert(in == 0);
/* Out was modified. But only after our custom assembly executed. */
assert(out == 2);
}

Expand All @@ -201,7 +204,7 @@ int main() {
and minimize the use of new registers.
*/
{
volatile int x = 0;
int x = 0;
asm (
"incl %0"
: "=r" (x)
Expand All @@ -214,12 +217,14 @@ int main() {
/*
# Specific register constraints
`a` forces it to be put into eax.
# a
Forces it to be put into eax.
*/
{
volatile int x = 0;
asm volatile (
"incl %0"
int x = 0;
asm (
"incl %%eax"
: "=a" (x)
: "0" (x)
);
Expand All @@ -235,11 +240,43 @@ int main() {
*/
{
register int eax asm ("eax");
asm volatile ("mov $1, %%eax;" : : : "%eax");
asm ("mov $1, %%eax;" : : : "%eax");
assert(eax == 1);
asm volatile ("mov $2, %%eax;" : : : "%eax");
asm ("mov $2, %%eax;" : : : "%eax");
assert(eax == 2);
}

/*
# modifiers
https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Modifiers.html#Modifiers
# =
Says that a value may have been modified by asm.
TODO: `=` vs indicating that it is an output? (before the second `:`?
# +
Both read from and written to.
*/

/*
# asm volatile
http://stackoverflow.com/questions/14449141/the-difference-between-asm-asm-volatile-and-clobbering-memory
TODO minimal example.
*/

/*
# volatile variable modified in asm
TODO do you need to mark variables modified in `asm` as volatile?
I think this is exactly what the `=` modifier does already, so maybe not.
*/
#endif
return EXIT_SUCCESS;
}
9 changes: 9 additions & 0 deletions gcc/gcc-as-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# GCC as library

How to use GCC as a library, e.g. from another C program

- <http://programmers.stackexchange.com/questions/189949/is-there-a-way-to-use-gcc-as-a-library>
- <http://stackoverflow.com/questions/1735360/compiling-program-within-another-program-using-gcc>
- <http://stackoverflow.com/questions/8144793/gcc-for-parsing-code>

`libgccjit` from GCC 5 goes a long way.
25 changes: 25 additions & 0 deletions opengl/alternatives.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,28 @@ Also by Khronos.
<http://gamedev.stackexchange.com/questions/96014/what-is-vulkan-and-how-does-it-differ-from-opengl>

Derived from <https://en.wikipedia.org/wiki/Mantle_%28API%29> by AMD, now abandoned in favor of Vulkan, and will somewhat be the new OpenGL.

There is no ES version like for OpenGL, <http://arstechnica.com/gadgets/2015/08/android-to-support-vulkan-graphics-api-the-open-answer-to-metal-and-dx12/> says ES and non-ES convergence has been going on for a while now.

### Samples

- <https://github.com/SaschaWillems/Vulkan>
- <https://github.com/LunarG/VulkanSamples>

### Hardware support

Supported hardware:

- NVIDIA <https://developer.nvidia.com/vulkan-driver>: Kepler and Maxwell
- Android
- API 24?
- Samsung Galaxy S7 was the first to support it.

Works on most GPUs that support OpenGL ES 3.1 <http://arstechnica.com/gadgets/2015/08/android-to-support-vulkan-graphics-api-the-open-answer-to-metal-and-dx12/>

### Ubuntu install

TODO. Already had an NVIDIA NVS 5400M driver 361.42 working for OpenGL.

- <http://askubuntu.com/questions/774131/installing-nvidia-vulkan-drivers-for-16-04>
- <https://github.com/SaschaWillems/Vulkan/issues/188>
1 change: 1 addition & 0 deletions posix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
1. [getrusage](dlopen.c)
1. [pthread](pthread.c)
1. [pthread_mutex](pthread_mutex.c)
1. [Regex](regex.c)
1. Networking
1. [socket](socket/)
1. [netdb.h](netdb-h.md)
Expand Down
4 changes: 4 additions & 0 deletions posix/bibliography.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
Very good intro do many of POSIX utilities with interesting examples and topics.

The problem is that each program requires you to pass several CLI arguments to them, and just print stuff to stdout, no assertions.

- <https://github.com/hailinzeng/Unix-Network-Programming>

- <http://www.apuebook.com/index.html> Advanced Programming in the UNIX® Environment, Third Edition by Richard Stevens
2 changes: 2 additions & 0 deletions posix/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

/* POSIX only headers. */
#include <arpa/inet.h>
#include <curses.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h> /* creat, O_CREAT */
Expand All @@ -67,6 +68,7 @@
#include <sys/utsname.h> /* uname, struct utsname */
#include <sys/wait.h> /* wait, sleep */
#include <syslog.h> /* syslog */
#include <termios.h>
#include <unistd.h> /* fork, ftruncate */

#define TMPFILE(x) __FILE__ "__" x ".tmp"
Expand Down
1 change: 1 addition & 0 deletions posix/curses/Makefile
1 change: 1 addition & 0 deletions posix/curses/Makefile_params
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LIBS := -lcurses
12 changes: 12 additions & 0 deletions posix/curses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# curses

1. [ncurses](ncurses.md)
1. Examples
1. [Hello world](hello.c)
1. printw
1. [printw newline](printw_newline.c)
1. [printw twice](printw_twice.c)
1. [printw newline screen height](printw_newline_screen_height.c)
1. [clear](clear.c)
1. [Check key pressed](check_key_pressed.c)
1. Canonical. TODO. <http://www.gnu.org/software/libc/manual/html_node/Canonical-or-Not.html>
59 changes: 59 additions & 0 deletions posix/curses/check_key_pressed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Check if a key was pressed in the last loop cycle:
http://stackoverflow.com/questions/2984307/c-key-pressed-in-linux-console
*/

#include "common.h"

/*
Return a character, on -1 if none was entered.
*/
int getkey(void) {
int character;
int stdin_fileno;
struct termios orig_term_attr;
struct termios new_term_attr;

stdin_fileno = fileno(stdin);

/* Save old attributes. */
tcgetattr(stdin_fileno, &orig_term_attr);

/* Set the terminal to raw mode. */
memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
/* TODO what are those parameters? */
new_term_attr.c_lflag &= ~(ECHO|ICANON);
new_term_attr.c_cc[VTIME] = 0;
new_term_attr.c_cc[VMIN] = 0;
tcsetattr(stdin_fileno, TCSANOW, &new_term_attr);

/* Read character. */
character = getchar();
while (getchar() != EOF);

/* Restore the original terminal attributes. */
tcsetattr(stdin_fileno, TCSANOW, &orig_term_attr);

return character;
}

int main(void) {
int key;
struct timespec sleep_cycle;
sleep_cycle.tv_sec = 0;
/*
If we reduce this a lot and hold da key, `-1` shows all the time.
When we hold a key, it it refreshed at a given low frequency it seems?
*/
sleep_cycle.tv_nsec = 500000000L;
initscr();
for (;;) {
clear();
key = getkey();
printw("%d\n", key);
refresh();
nanosleep(&sleep_cycle, NULL);
}
endwin();
return 0;
}
16 changes: 16 additions & 0 deletions posix/curses/clear.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Second times overwrites the first. */

#include "common.h"
#include "common_curses.h"

int main(void) {
int c;
initscr();
printw("before clear\n");
clear();
printw("after clear\n");
refresh();
press_any_key_to_quit();
endwin();
return 0;
}
1 change: 1 addition & 0 deletions posix/curses/common.h
7 changes: 7 additions & 0 deletions posix/curses/common_curses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define ANY_KEY_TO_QUIT_MSG "press any key to quit\n"

void press_any_key_to_quit() {
printw(ANY_KEY_TO_QUIT_MSG);
refresh();
getch();
}
13 changes: 13 additions & 0 deletions posix/curses/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <curses.h>

int main(void) {
/* Startup. */
initscr();
printw("hello world. Press any key to quit.");
refresh();
/* Wait for use input. */
getch();
/* Teardown. */
endwin();
return 0;
}
7 changes: 7 additions & 0 deletions posix/curses/ncurses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ncurses

Name of the dominant implementation.

POSIX only talks about curses however.

<http://stackoverflow.com/questions/1517756/whats-the-difference-between-lcurses-and-lncurses-when-compiling-c-using-ncur>
12 changes: 12 additions & 0 deletions posix/curses/printw_newline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* Newlines do move the cursor down and to the beginning of the next line. */

#include "common.h"

int main(void) {
initscr();
printw("a\nb\npress any key to quit");
refresh();
getch();
endwin();
return 0;
}
18 changes: 18 additions & 0 deletions posix/curses/printw_newline_screen_height.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Bad things happen: at the bottom of the screen,
the cursor is imply not lowered anymore.
*/

#include "common.h"
#include "common_curses.h"

int main(void) {
int i;
initscr();
for (i = 0; i < 1000; ++i) {
printw("%d\n", i);
}
press_any_key_to_quit();
endwin();
return 0;
}
15 changes: 15 additions & 0 deletions posix/curses/printw_twice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* The current cursor position is unchanged. */

#include "common.h"
#include "common_curses.h"

int main(void) {
initscr();
printw("first\n");
printw("second\n");
printw(ANY_KEY_TO_QUIT_MSG);
refresh();
getch();
endwin();
return 0;
}
2 changes: 1 addition & 1 deletion posix/environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "common.h"

int main() {
int main(void) {
/* Print entire environment. */
extern char **environ;
char **env = environ;
Expand Down
2 changes: 1 addition & 1 deletion posix/interactive/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "common.h"

int main() {
int main(void) {
int i = 0;
while (1) {
printf("%d\n", i);
Expand Down
Loading

0 comments on commit e52ed4b

Please sign in to comment.